简体   繁体   English

ImportError:没有名为的模块 - Python

[英]ImportError: No module named - Python

I have a python application with the following directory structure: 我有一个python应用程序具有以下目录结构:

src
 |
 +---- main
 |
 +---- util
 |
 +---- gen_py
         |
         +---- lib

In the package main , I have a python module named MyServer.py which has an import statement like: 在包main中 ,我有一个名为MyServer.py的python模块,它有一个import语句,如:

from gen_py.lib import MyService

In order for this statement to work, I placed the following line at the beginning of MyServer.py : 为了使这个语句起作用,我将以下行放在MyServer.py的开头:

import sys
sys.path.append('../gen_py/lib')

When I run MyServer.py in the terminal, I get the following error: 当我在终端中运行MyServer.py时,出现以下错误:

ImportError: No module named gen_py.lib ImportError:没有名为gen_py.lib的模块

What I am missing here? 我在这里缺少什么?

Your modification of sys.path assumes the current working directory is always in main/ . 您对sys.path修改假定当前工作目录始终位于main/ This is not the case. 不是这种情况。 Instead, just add the parent directory to sys.path : 相反,只需将父目录添加到sys.path

import sys
import os.path

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import gen_py.lib

Don't forget to include a file __init__.py in gen_py and lib - otherwise, they won't be recognized as Python modules. 不要忘记在gen_py和lib中包含文件__init__.py - 否则,它们将不会被识别为Python模块。

For the Python module import to work, you must have "src" in your path, not "gen_py/lib". 要使Python模块导入起作用,您的路径中必须有“src”,而不是“gen_py / lib”。

When processing an import like import gen_py.lib , it looks for a module gen_py , then looks for a submodule lib . 处理import gen_py.lib类的import gen_py.lib ,它会查找模块gen_py ,然后查找子模块lib

As the module gen_py won't be in "../gen_py/lib" (it'll be in ".."), the path you added will do nothing to help the import process. 由于模块gen_py不在“../gen_py/lib”中(它将在“..”中),因此您添加的路径将无助于导入过程。

Depending on where you're running it from, try adding the relative path to the "src" folder. 根据您运行它的位置,尝试添加“src”文件夹的相对路径。 Perhaps it's sys.path.append('..') . 也许是sys.path.append('..') You might also have success running the script while inside the src folder directly, via relative paths like python main/MyServer.py 您可能也可以通过python main/MyServer.py类的相对路径直接在src文件夹中运行脚本

from ..gen_py.lib import MyService

or 要么

from main.gen_py.lib import MyService

Make sure you have a (at least empty) __init__.py file on each directory. 确保每个目录上都有一个(至少为空的) __init__.py文件。

确保包含__init__.py ,这使Python知道那些目录包含包

This is if you are building a package and you are finding error in imports. 如果您正在构建一个包,并且您在导入中发现错误。 I learnt it the hard way.The answer isn't to add the package to python path or to do it programatically (what if your module gets installed and your command adds it again?) thats a bad way. 我学到了很难的方法。答案不是将包添加到python路径或以编程方式执行(如果你的模块已经安装并且你的命令再次添加它会怎样?)这是一种糟糕的方式。

The right thing to do is: 1) Use virtualenv pyvenv-3.4 or something similar 2) Activate the development mode - $python setup.py develop 正确的做法是:1)使用virtualenv pyvenv-3.4或类似的东西2)激活开发模式 - $ python setup.py develop

Make sure if root project directory is coming up in sys.path output. 确保root项目目录是否出现在sys.path输出中。 If not, please add path of root project directory to sys.path. 如果没有,请将根项目目录的路径添加到sys.path。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM