简体   繁体   English

Python:使用命令行中的相对路径运行脚本

[英]Python: run script with relative path from command line

TL;DR TL; DR

What are the options on Windows 10 to run from command line a python script specified by a relative path? Windows 10上有哪些选项可以从命令行运行由相对路径指定的python脚本? Can environment variables be changed to do so? 可以更改环境变量吗? How can this be done? 如何才能做到这一点?

LONG

I am using Windows 10. When I follow this installation guide (written for Linux), everything is fine until I need to perform the following on Windows 我正在使用Windows10。当我遵循安装指南(为Linux编写)时,一切正常,直到需要在Windows上执行以下操作为止

# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

On Windows, I added the two paths (the entire, absolute, path) to PYTHONPATH. 在Windows上,我向PYTHONPATH添加了两个路径(完整路径,绝对路径)。 When I execute the following; 当我执行以下命令时; I see the extra two paths added. 我看到添加了额外的两个路径。

C:\User\me>    python -c "import sys; print(sys.path)"

However, if I create a simple test.py script 但是,如果我创建一个简单的test.py脚本

print("Relative path works")

and run it from the command line: 并从命令行运行它:

C:\User\me> python test.py

it works if the test.py file is saved in C:\\User\\me\\, but NOT when it is saved in the folders of PYTHONPATH. 如果将test.py文件保存在C:\\ User \\ me \\中,则它起作用,但是当将它保存在PYTHONPATH文件夹中时,该方法无效。

This SO Q&A says that 这样的问与答

PYTHONPATH is used by the python interpreter. PYTHONPATH由python解释器使用。 It is not the same as Windows' PATH environment variable. 它与Windows的PATH环境变量不同。 You can't use it as a search path for passing files to the interpreter on the command line. 您不能将其用作在命令行上将文件传递到解释器的搜索路径。

But I could not find how I can achieve what I need, using relative path of script to run it from command line. 但是我找不到如何使用脚本的相对路径从命令行运行它来实现所需的功能。 Please note that using the absolute path is not an option, because the instalation guide requires 2 folders are added to the PYTHONPATH, in order for the task to work. 请注意,使用绝对路径不是一种选择,因为安装指南要求将2个文件夹添加到PYTHONPATH中,以使任务正常运行。

It should be possible but I did not find any answer - Python 3 documentation describes Command line option for running script with relative path: 应该可以,但是我没有找到任何答案-Python 3文档描述了使用相对路径运行脚本的命令行选项:

Execute the Python code contained in script, which must be a filesystem path (absolute or relative) referring to either a Python file... 执行脚本中包含的Python代码,该代码必须是引用Python文件的文件系统路径(绝对或相对)。

These lines from the install guide are intended to be run after a cd .../tensorflow/models/research/ and thus are absolute paths: 安装指南中的这些行旨在在cd .../tensorflow/models/research/ ,因此是绝对路径:

 # From tensorflow/models/research/ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim 

You will likely need to do something similar on Windows. 您可能需要在Windows上执行类似的操作。 The equivalent would likely be to add the absolute paths to the libraries to PYTHONPATH environment variable. 等效方法可能是将库的绝对路径添加到PYTHONPATH环境变量。

I haven't tested this but as mentioned in here , this >>> export PYTHONPATH=$PYTHONPATH: pwd : pwd /slim should be used as 我没有测试过,但是正如这里提到的,这>>> export PYTHONPATH = $ PYTHONPATH: pwdpwd / slim应该用作

set PYTHONPATH=%PYTHONPATH%;%cd%;%cd%\\slim 设置PYTHONPATH =%PYTHONPATH%;%cd%;%cd%\\ slim

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

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