如何在命令行中使用 Python 脚本而无需 cd-ing 到其目录?是 PYTHONPATH 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19917492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use a Python script in the command line without cd-ing to its directory? Is it the PYTHONPATH?
提问by Randy Skretka
How can I make any use of PYTHONPATH? When I try to run a script in the path the file is not found. When I cd to the directory holding the script the script runs. So what good is the PYTHONPATH?
如何使用 PYTHONPATH?当我尝试在路径中运行脚本时,找不到文件。当我 cd 到保存脚本的目录时,脚本会运行。那么 PYTHONPATH 有什么好处呢?
$ echo $PYTHONPATH
:/home/randy/lib/python
$ tree -L 1 '/home/randy/lib/python'
/home/randy/lib/python
├── gbmx_html.py
├── gbmx.py
├── __init__.py
├── __pycache__
├── scripts
└── yesno.py
$ python gbmx.py -h
python: can't open file 'gbmx.py': [Errno 2] No such file or directory
$ cd '/home/randy/lib/python'
After cd to the file directory it runs ..
在 cd 到文件目录后,它运行 ..
$ python gbmx.py -h
usage: gbmx.py [-h] [-b]
Why can I not make any use of the PYTHONPATH?
为什么我不能使用 PYTHONPATH?
采纳答案by Pedro Werneck
I think you're a little confused. PYTHONPATH sets the search path for importingpython modules, not for executing them like you're trying.
我觉得你有点糊涂了。PYTHONPATH 设置用于导入python 模块的搜索路径,而不是像您尝试的那样执行它们。
PYTHONPATH Augment the default search path for module files. The format is the same as the shell's PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.
In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.
The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.
An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.
PYTHONPATH 增加模块文件的默认搜索路径。格式与shell 的PATH 相同:一个或多个由os.pathsep 分隔的目录路径名(例如Unix 上的冒号或Windows 上的分号)。不存在的目录将被静默忽略。
除了普通目录之外,单个 PYTHONPATH 条目可能会引用包含纯 Python 模块(源代码或编译形式)的 zipfile。不能从 zipfiles 导入扩展模块。
默认搜索路径取决于安装,但通常以 prefix/lib/pythonversion 开头(请参阅上面的 PYTHONHOME)。它始终附加到 PYTHONPATH。
一个额外的目录将被插入到 PYTHONPATH 前面的搜索路径中,如上面接口选项下所述。搜索路径可以在 Python 程序中作为变量 sys.path 进行操作。
http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
What you're looking for is PATH.
你要找的是PATH。
export PATH=$PATH:/home/randy/lib/python
However, to run your python script as a program, you also need to set a shebangfor Python in the first line. Something like this should work:
但是,要将 Python 脚本作为程序运行,您还需要在第一行中为 Python设置一个shebang。这样的事情应该工作:
#!/usr/bin/env python
And give execution privileges to it:
并赋予它执行权限:
chmod +x /home/randy/lib/python/gbmx.py
Then you should be able to simply run gmbx.py
from anywhere.
那么你应该能够简单地gmbx.py
从任何地方运行。
回答by Richard
You're confusing PATH and PYTHONPATH. You need to do this:
你混淆了 PATH 和 PYTHONPATH。你需要这样做:
export PATH=$PATH:/home/randy/lib/python
PYTHONPATH is used by the python interpreter to determine which modules to load.
Python 解释器使用 PYTHONPATH 来确定要加载哪些模块。
PATH is used by the shell to determine which executables to run.
shell 使用 PATH 来确定要运行的可执行文件。
回答by codedstructure
PYTHONPATH
only affects import
statements, not the top-level Python interpreter's lookup of python files given as arguments.
PYTHONPATH
仅影响import
语句,而不影响顶级 Python 解释器对作为参数给出的 Python 文件的查找。
Needing PYTHONPATH
to be set is not a great idea - as with anything dependent on environment variables, replicating things consistently across different machines gets tricky. Better is to use Python 'packages' which can be installed (using 'pip', or distutils) in system-dependent paths which Python already knows about.
需要PYTHONPATH
设置并不是一个好主意——因为任何依赖于环境变量的东西,在不同的机器上一致地复制东西变得棘手。更好的是使用 Python 'packages',它可以安装(使用 'pip' 或 distutils)在 Python 已经知道的依赖于系统的路径中。
Have a read of https://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/- 'The Hitchhiker's Guide to Packaging', and also http://docs.python.org/3/tutorial/modules.html- which explains PYTHONPATH and packages at a lower level.
阅读https://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/- 'The Hitchhiker's Guide to Packaging',以及http://docs.python.org/3/tutorial /modules.html- 在较低级别解释 PYTHONPATH 和包。
回答by W4t3randWind
I think you're mixed up between PATH and PYTHONPATH. All you have to do to run a 'script' is have it's parental directory appended to your PATH variable. You can test this by running
我认为您在 PATH 和 PYTHONPATH 之间混淆了。运行“脚本”所需要做的就是将它的父目录附加到您的 PATH 变量中。您可以通过运行来测试
which myscript.py
Also, if myscripy.py
depends on custom modules, their parental directories must also be added to the PYTHONPATH variable. Unfortunately, because the designers of python were clearly on drugs, testing your imports in the repl with the following will not guarantee that your PYTHONPATH is set properly for use in a script. This part of python programming is magic and can't be answered appropriately on stackoverflow.
此外,如果myscripy.py
依赖于自定义模块,则还必须将其父目录添加到 PYTHONPATH 变量中。不幸的是,因为 python 的设计者显然是在吸毒,所以使用以下内容在 repl 中测试您的导入并不能保证您的 PYTHONPATH 设置正确以在脚本中使用。Python 编程的这部分很神奇,在 stackoverflow 上无法正确回答。
$python
Python 2.7.8 blahblahblah
...
>from mymodule.submodule import ClassName
>test = ClassName()
>^D
$myscript_that_needs_mymodule.submodule.py
Traceback (most recent call last):
File "myscript_that_needs_mymodule.submodule.py", line 5, in <module>
from mymodule.submodule import ClassName
File "/path/to/myscript_that_needs_mymodule.submodule.py", line 5, in <module>
from mymodule.submodule import ClassName
ImportError: No module named submodule
回答by Tigran Saluev
With PYTHONPATH set as in your example, you should be able to do
在您的示例中设置 PYTHONPATH 后,您应该能够做到
python -m gmbx
-m
option will make Python search for your module in paths Python usually searches modules in, including what you added to PYTHONPATH. When you run interpreter like python gmbx.py
, it looks for particular file and PYTHONPATH does not apply.
-m
选项将使 Python 在路径中搜索您的模块 Python 通常在其中搜索模块,包括您添加到 PYTHONPATH 的内容。当您运行解释器 like 时python gmbx.py
,它会查找特定文件并且 PYTHONPATH 不适用。