首页 > 解决方案 > ModuleNotFoundError:没有名为“my_app”的模块。即使使用 __init__.py

问题描述

这是我的项目结构。

├─demo_data
├─findb
│  └─__pycache__
├─management
└─tw_equity
    ├─migrations
    │  └─__pycache__       
    └─__pycache__

现在我尝试将演示数据插入到我的模型中。所以我commands.py为这个过程创造。

# ./management/commands.py

from datetime import datetime
from tw_equity.models import (
    TradingDate,
    Basic,
    Price,
    Holder,
)

with open('demo_data/trading_date.csv') as f:

    for row in f:
        data = datetime.strptime(row, "%Y-%m-%d").date()
        trading_date = TradingDate(
            trading_date=data
        )
        trading_date.save()

    print('Insert Successfully.')

但是错误不断出现。

(Findb) C:\Users\ycyta\python-django-Findb>management\commands.py
Traceback (most recent call last):
  File "C:\Users\ycyta\python-django-Findb\management\commands.py", line 2, in <module>
    from tw_equity.models import (
ModuleNotFoundError: No module named 'tw_equity'

已经试过了

  1. python manage.py shell,一切都很好。
(Findb) C:\Users\ycyta\python-django-Findb>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from tw_equity.models import TradingDate
>>> TradingDate
<class 'tw_equity.models.TradingDate'>
  1. 以前大多数类似的问题都被关注__ini__.py,但我已经有了。

项目文件夹详细信息


感谢您的帮助。

标签: pythondjangomodulemodel

解决方案


推荐阅读