问题:如何在不使用CD-cd进入目录的情况下在命令行中使用Python脚本?是PYTHONPATH吗?

如何使用PYTHONPATH?当我尝试在路径中运行脚本时,找不到文件。当我进入包含该脚本的目录时,该脚本将运行。那么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'

cd到文件目录后,它运行..

$ python gbmx.py -h
usage: gbmx.py [-h] [-b]

为什么我不能使用PYTHONPATH?

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?

$ 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 ..

$ python gbmx.py -h
usage: gbmx.py [-h] [-b]

Why can I not make any use of the PYTHONPATH?


回答 0

我觉得你有点困惑。PYTHONPATH设置用于导入 python模块的搜索路径,而不是像您尝试的那样执行它们。

PYTHONPATH扩展模块文件的默认搜索路径。格式与Shell的PATH相同:一个或多个目录路径名,以os.pathsep分隔(例如Unix上的冒号或Windows上的分号)。不存在的目录将被静默忽略。

除了普通目录外,单独的PYTHONPATH条目还可以引用包含纯Python模块(以源代码或编译形式)的zipfile。无法从zip文件导入扩展模块。

默认搜索路径取决于安装,但通常以prefix / lib / pythonversion开头(请参见上面的PYTHONHOME)。它总是附加在PYTHONPATH上。

如上文“接口选项”下所述,将在PYTHONPATH前面的搜索路径中插入一个附加目录。可以从Python程序中将搜索路径作为变量sys.path进行操作。

http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

您正在寻找的是PATH。

export PATH=$PATH:/home/randy/lib/python 

但是,要将Python脚本作为程序运行,您还需要在第一行中为Python 设置shebang。这样的事情应该起作用:

#!/usr/bin/env python

并赋予执行特权:

chmod +x /home/randy/lib/python/gbmx.py

然后,您应该可以轻松地gmbx.py从任何地方运行。

I think you’re a little confused. PYTHONPATH sets the search path for importing python modules, not for executing them like you’re trying.

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.

http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

What you’re looking for is PATH.

export PATH=$PATH:/home/randy/lib/python 

However, to run your python script as a program, you also need to set a shebang for Python in the first line. Something like this should work:

#!/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.


回答 1

您混淆了PATH和PYTHONPATH。您需要这样做:

export PATH=$PATH:/home/randy/lib/python 

python解释器使用PYTHONPATH确定要加载的模块。

Shell使用PATH来确定要运行的可执行文件。

You’re confusing PATH and PYTHONPATH. You need to do this:

export PATH=$PATH:/home/randy/lib/python 

PYTHONPATH is used by the python interpreter to determine which modules to load.

PATH is used by the shell to determine which executables to run.


回答 2

PYTHONPATH仅影响import语句,而不影响顶级Python解释器对作为参数给出的python文件的查找。

需要PYTHONPATH设置不是一个好主意-就像任何依赖于环境变量的东西一样,在不同的机器上一致地复制东西变得棘手。更好的方法是使用可以在Python已经知道的系统相关路径中安装(使用’pip’或distutils)的Python’packages’。

阅读https://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/- “ The Hitchhiker’s Packaging Guide”,以及http://docs.python.org/3/tutorial /modules.html-在较低级别解释PYTHONPATH和软件包。

PYTHONPATH only affects import statements, not the top-level Python interpreter’s lookup of python files given as arguments.

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.

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.


回答 3

我认为您介于PATH和PYTHONPATH之间。运行“脚本”所需要做的就是将其父目录附加到PATH变量中。您可以通过运行来测试

which myscript.py

另外,如果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

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

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.

$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

回答 4

如示例中那样设置PYTHONPATH,您应该可以

python -m gmbx

-m选项将使Python在路径中搜索模块,而Python通常会在其中搜索模块,包括您添加到PYTHONPATH中的模块。当您运行像这样的解释器时python gmbx.py,它将查找特定文件,而PYTHONPATH不适用。

With PYTHONPATH set as in your example, you should be able to do

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。