I'm developing/testing a package in my local directory. I want to import it in the interpreter (v2.5), but sys.path does not include the current directory. Right now I type in sys.path.insert(0,'.')
. Is there a better way?
我正在我的本地目录中开发/测试包。我想在解释器(v2.5)中导入它,但sys.path不包含当前目录。现在我输入sys.path.insert(0,'。')。有没有更好的办法?
Also,
也,
from . import mypackage
fails with this error:
失败并出现此错误:
ValueError: Attempted relative import in non-package
6 个解决方案
#1
30
You can use relative imports only from in a module that was in turn imported as part of a package -- your script or interactive interpreter wasn't, so of course from . import
(which means "import from the same package I got imported from") doesn't work. import mypackage
will be fine once you ensure the parent directory of mypackage
is in sys.path
(how you managed to get your current directory away from sys.path
I don't know -- do you have something strange in site.py, or...?)
您只能在模块中使用相对导入,而该模块又作为包的一部分导入 - 您的脚本或交互式解释器当然不是。 import(表示“从我从中导入的同一个包导入”)不起作用。一旦确保mypackage的父目录在sys.path中(你如何设法让你的当前目录远离sys.path我不知道 - 你在site.py中有什么奇怪的东西,或者......?)
To get your current directory back into sys.path
there is in fact no better way than putting it there;-).
要将当前目录恢复到sys.path,实际上没有比把它放在那里更好的方法;-)。
#2
12
See the documentation for sys.path:
请参阅sys.path的文档:
http://docs.python.org/library/sys.html#sys.path
http://docs.python.org/library/sys.html#sys.path
To quote:
去引用:
If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.
如果脚本目录不可用(例如,如果以交互方式调用解释器或者从标准输入读取脚本),则路径[0]为空字符串,它指示Python首先搜索当前目录中的模块。
So, there's no need to monkey with sys.path if you're starting the python interpreter from the directory containing your module.
因此,如果您从包含模块的目录启动python解释器,则无需使用sys.path。
Also, to import your package, just do:
另外,要导入您的包,只需执行以下操作:
import mypackage
Since the directory containing the package is already in sys.path, it should work fine.
由于包含该包的目录已经在sys.path中,因此它应该可以正常工作。
#3
5
Keep it simple:
把事情简单化:
try:
from . import mymodule # "myapp" case
except:
import mymodule # "__main__" case
#4
5
If you want to run an unmodified python script so it imports libraries from a specific local directory you can set the PYTHONPATH
environment variable - e.g. in bash:
如果要运行未修改的python脚本以便从特定的本地目录导入库,则可以设置PYTHONPATH环境变量 - 例如在bash中:
export PYTHONPATH=/home/user/my_libs
python myscript.py
If you just want it to import from the current working directory use the .
notation:
如果您只想从当前工作目录导入它,请使用。符号:
export PYTHONPATH=.
python myscript.py
#5
4
A simple way to make it work is to run your script from the parent directory using python's -m
flag, e.g. python -m packagename.scriptname
. Obviously in this situation you need an __init__.py
file to turn your directory into a package.
使其工作的一种简单方法是使用python的-m标志从父目录运行脚本,例如python -m packagename.scriptname。显然,在这种情况下,您需要一个__init__.py文件来将您的目录转换为一个包。
#6
2
Using sys.path
should include current directory already.
使用sys.path应该包含当前目录。
Try:
尝试:
import .
or:
要么:
from . import sth
however it may be not a good practice, so why not just use:
然而,这可能不是一个好习惯,所以为什么不使用:
import mypackage
#1
30
You can use relative imports only from in a module that was in turn imported as part of a package -- your script or interactive interpreter wasn't, so of course from . import
(which means "import from the same package I got imported from") doesn't work. import mypackage
will be fine once you ensure the parent directory of mypackage
is in sys.path
(how you managed to get your current directory away from sys.path
I don't know -- do you have something strange in site.py, or...?)
您只能在模块中使用相对导入,而该模块又作为包的一部分导入 - 您的脚本或交互式解释器当然不是。 import(表示“从我从中导入的同一个包导入”)不起作用。一旦确保mypackage的父目录在sys.path中(你如何设法让你的当前目录远离sys.path我不知道 - 你在site.py中有什么奇怪的东西,或者......?)
To get your current directory back into sys.path
there is in fact no better way than putting it there;-).
要将当前目录恢复到sys.path,实际上没有比把它放在那里更好的方法;-)。
#2
12
See the documentation for sys.path:
请参阅sys.path的文档:
http://docs.python.org/library/sys.html#sys.path
http://docs.python.org/library/sys.html#sys.path
To quote:
去引用:
If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.
如果脚本目录不可用(例如,如果以交互方式调用解释器或者从标准输入读取脚本),则路径[0]为空字符串,它指示Python首先搜索当前目录中的模块。
So, there's no need to monkey with sys.path if you're starting the python interpreter from the directory containing your module.
因此,如果您从包含模块的目录启动python解释器,则无需使用sys.path。
Also, to import your package, just do:
另外,要导入您的包,只需执行以下操作:
import mypackage
Since the directory containing the package is already in sys.path, it should work fine.
由于包含该包的目录已经在sys.path中,因此它应该可以正常工作。
#3
5
Keep it simple:
把事情简单化:
try:
from . import mymodule # "myapp" case
except:
import mymodule # "__main__" case
#4
5
If you want to run an unmodified python script so it imports libraries from a specific local directory you can set the PYTHONPATH
environment variable - e.g. in bash:
如果要运行未修改的python脚本以便从特定的本地目录导入库,则可以设置PYTHONPATH环境变量 - 例如在bash中:
export PYTHONPATH=/home/user/my_libs
python myscript.py
If you just want it to import from the current working directory use the .
notation:
如果您只想从当前工作目录导入它,请使用。符号:
export PYTHONPATH=.
python myscript.py
#5
4
A simple way to make it work is to run your script from the parent directory using python's -m
flag, e.g. python -m packagename.scriptname
. Obviously in this situation you need an __init__.py
file to turn your directory into a package.
使其工作的一种简单方法是使用python的-m标志从父目录运行脚本,例如python -m packagename.scriptname。显然,在这种情况下,您需要一个__init__.py文件来将您的目录转换为一个包。
#6
2
Using sys.path
should include current directory already.
使用sys.path应该包含当前目录。
Try:
尝试:
import .
or:
要么:
from . import sth
however it may be not a good practice, so why not just use:
然而,这可能不是一个好习惯,所以为什么不使用:
import mypackage