简体   繁体   English

Python3错误“ImportError:没有名为的模块”有__init__.py

[英]Python3 error “ImportError: No module named” have __init__.py

I created a simple flask application and my directory structure looks like this: 我创建了一个简单的烧瓶应用程序,我的目录结构如下所示:

myproject
- src
-- models
--- __init__.py
-- views
--- errors.py
--- default.py
-- application.py
-- config.py
-- __init__.py
- test
-- test_myproject.py
Readme.md
setup.py
..

The application.py from the src look like this: 来自src的application.py看起来像这样:

from src.models import db
from src.views.errors import error_pages
from src.views.scoreboard import scoreboard
from src.config import config

The test_myproject looks like this: test_myproject看起来像这样:

from src.models import db, Result
from src.application import create_app

I use Pycharm to develop this and when I click run it works but when I try to run it via the command line python application.py I get the following error: 我使用Pycharm来开发它,当我点击运行它工作,但当我尝试通过命令行python application.py运行它时,我收到以下错误:

Traceback (most recent call last):
  File "src/application.py", line 18, in <module>
    from src.models import db
ImportError: No module named 'src'

Runnint the unittests with python -m unittest works. 使用python -m unittest工作运行单元测试。

If I change the imports from application.py to: 如果我将application.py中的导入更改为:

from models import db
from views.errors import error_pages
from views.scoreboard import scoreboard
from config import config

It will work when I run it via the command line, however the tests won't work anymore. 当我通过命令行运行它时它会工作,但是测试将不再起作用。

File ".../src/application.py", line 18, in <module>
    from models import db
ImportError: No module named 'models'

What am I doing wrong? 我究竟做错了什么?

You said that you're running python application.py , which suggests that your working directory is src . 你说你正在运行python application.py ,这表明你的工作目录是src To be able to import modules under src.* (without playing with PYTHONPATH) you should run Python from the myproject directory instead. 为了能够在src.*下导入模块(不使用PYTHONPATH),你应该从myproject目录运行Python。

Consider either moving application.py to myproject or running it with python src/application.py . 考虑将application.py移动到myproject或使用python src/application.py运行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ImportError:__init__.py文件中没有名为…的模块 - ImportError: No module named… in __init__.py file ImportError:没有名为…的模块(但是我有__init__.py文件并设置了PYTHONPATH) - ImportError: No module named … (but I have __init__.py files and have set PYTHONPATH) ImportError:当包含___init__.py时,没有名为test的模块 - ImportError: No module named test when ___init__.py is included ImportError:在__init__.py文件Python中导入类时没有名为''的模块 - ImportError: No module named ' ' while Import class in the __init__.py file Python ImportError:没有名为ex47.game的模块(是的,我有一个__init__.py文件*并且*我尝试了所有PYTHONPATH的东西 - ImportError: No module named ex47.game (and yes, I have an __init__.py file *AND* I tried all that PYTHONPATH stuff 接收导入错误:没有名为***的模块,但有__init__.py - Receiving Import Error: No Module named ***, but has __init__.py __init__.py中的导入错误:PyVlfeat中没有名为_vlfeat的模块 - Import Error in __init__.py : No Module named _vlfeat in PyVlfeat Python:当 __init__.py 存在时导入错误 - Python: ImportError while __init__.py exists 文件“ /usr/lib/python2.7/importlib/__init__.py”,第37行,位于import_module __import __(name)ImportError中:没有名为django的模块 - File “/usr/lib/python2.7/importlib/__init__.py”, line 37, in import_module __import__(name) ImportError: No module named django Python - 没有名为错误的模块。 无论我将 __init__.py 文件放在哪里,摄取器文件似乎都无法访问它 - Python- no module named error. wherever i place the __init__.py file the ingestor file just cant seem to get at it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM