[英]Can't import own packages in Python 2.7
I'm having some trouble importing own packages in my programs, and so I made a test folder to try and understand what I'm doing wrong. 我在将自己的程序包导入程序时遇到了一些麻烦,因此我创建了一个测试文件夹以尝试了解我在做什么错。
It's the simplest of things, But I still can't get it to work. 这是最简单的事情,但我仍然无法正常工作。
This is my folder structure: 这是我的文件夹结构:
test
> pack1
> __init__.py
> mod1.py
> pack2
> __init__.py
> mod2.py
Both init
-files are empty. 两个
init
文件都为空。
mod1
looks like this: mod1
看起来像这样:
def foo():
print "hello"
and mod2
looks like this 和
mod2
看起来像这样
from pack1.mod1 import *
foo()
When running the code in PyCharm, everything works fine! 在PyCharm中运行代码时,一切正常! But when trying to execute from cmd I get
ImportError: No module named pack1.mod1
但是当尝试从cmd执行时,我得到
ImportError: No module named pack1.mod1
Is sys.path.insert(0, "../pack1")
my only option, or is there another reason why cmd will not cooperate? 是
sys.path.insert(0, "../pack1")
我唯一的选择,还是cmd无法配合使用的另一个原因?
Regardless of version, python has to know where to look for packages. 无论版本如何,python都必须知道在哪里寻找软件包。 Manipulating
sys.path
is a quick and dirty option, which will break sometimes in the future, if your code grows more complex. 操纵
sys.path
是一个快速而肮脏的选择,如果您的代码变得更加复杂,将来会有时中断。 Try making a package and install it via pip install -e
or python setup.py develop
尝试制作一个软件包并通过
pip install -e
或python setup.py develop
pip install -e
(Look for this at the nice distutils introduction ) (在漂亮的distutils简介中查找此内容)
In regular Python, there are only certain folders that are checked for importing packages and the test
folder you have doesn't seem to be one of those files. 在常规Python中,只有某些文件夹会被检查以导入软件包,而您拥有的
test
文件夹似乎不是这些文件之一。 To change this, edit sys.path
in mod2.py
and then import pack1.mod1
. 要更改此设置,请在
mod2.py
编辑sys.path
, 然后导入pack1.mod1
。
mod2.py
import sys
# Add test folder to sys.path
sys.path.append("../")
from pack1.mod1 import *
# Prints "hello"!
foo()
Also, instead of editing sys.path
, you could add the pack1
folder into the Lib
folder within your Python directory. 另外,除了编辑
sys.path
,您还可以将pack1
文件夹添加到Python目录中的Lib
文件夹中。 This will work because this is, by default, one of the folders in sys.path
. 这将起作用,因为默认情况下,这是
sys.path
中的文件夹之一。
Python 2.7
> Lib
> pack1
> __init__.py
> mod1.py
mod2.py
from pack1.mod1 import *
# Prints "hello"!
foo()
You say you execute it via: (Documents)/test/pack2> python mod2.py
您说您通过以下方式执行它:
(Documents)/test/pack2> python mod2.py
Problem is that pack2.mod2.py
doesn't know where pack1
is. 问题是
pack2.mod2.py
不知道pack1
在哪里。
Execute it as module: (Documents)/test> python -m pack2.mod2
作为模块执行:(
(Documents)/test> python -m pack2.mod2
If you do not want to modify scripts or directory layout you can use PYTHONPATH
environmental variable. 如果您不想修改脚本或目录布局,则可以使用
PYTHONPATH
环境变量。
Example 例
vagrant@h:/tmp/test/pack2$ python mod2.py
Traceback (most recent call last):
File "mod2.py", line 1, in <module>
from pack1.mod1 import *
ImportError: No module named pack1.mod1
vagrant@h:/tmp/test/pack2$ export PYTHONPATH="${PYTHONPATH}:/tmp/test"
vagrant@h:/tmp/test/pack2$ python mod2.py
hello
vagrant@h:/tmp/test/pack2$
More about searching modules - https://docs.python.org/2/tutorial/modules.html#the-module-search-path 有关搜索模块的更多信息-https: //docs.python.org/2/tutorial/modules.html#the-module-search-path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.