[英]How can I add a python script to the windows system path?
I'm using windows cmd to run my python script.我正在使用 windows cmd 来运行我的 python 脚本。 I want to run my python script withouth to give the cd command and the directory path.
我想在不提供 cd 命令和目录路径的情况下运行我的 python 脚本。 I would like to type only the name of the python script and run it.
我只想输入 python 脚本的名称并运行它。
I'm using python 2.7我正在使用 python 2.7
1.Go to Environmental Variables > system variable > Path > Edit 1.转到环境变量 > 系统变量 > 路径 > 编辑
2.It look like this 2.看起来像这样
Path C:\\Program Files\\Java\\jdk1.8.0\\bin;%SystemRoot%\\system32;C:\\Program Files\\nodejs\\; 路径C:\\ Program Files \\ Java \\ jdk1.8.0 \\ bin;%SystemRoot%\\ system32; C:\\ Program Files \\ nodejs \\;
3.You can add semicolon (;) at the end and add C:\\Python27 3.您可以在末尾添加分号(;)并添加C:\\ Python27
4.After adding it look like this 4.添加后看起来像这样
C:\\Program Files\\Java\\jdk1.8.0\\bin;%SystemRoot%\\system32;C:\\Program Files\\nodejs\\;C:\\Python27; C:\\ Program Files \\ Java \\ jdk1.8.0 \\ bin;%SystemRoot%\\ system32; C:\\ Program Files \\ nodejs \\; C:\\ Python27;
Create batch file inside the script's directory.在脚本目录中创建批处理文件。
@echo off
echo.
python %~dp0\<script-name>.py %*
Put the above lines inside of it.把上面的几行放在里面。 And change according the script you want to run.
并根据您要运行的脚本进行更改。
" %~dp0 " is unique variable witch returns batch file's directory. “ %~dp0 ”是返回批处理文件目录的唯一变量。
" % *" are the arguments passed through. “ % *” 是传递的参数。
os.chdir(os.path.dirname(sys.argv[0]))
Also put the above line inside the script.还将上面的行放在脚本中。 The code above changes your python script's working directory to it's own directory.
上面的代码将您的 python 脚本的工作目录更改为它自己的目录。 "sys.argv[0]" always returns path of the script itself.
“sys.argv[0]”总是返回脚本本身的路径。
And add your script to Enviroment Variables.并将您的脚本添加到环境变量中。 And run from command prompt using the batch file's name.
并使用批处理文件的名称从命令提示符运行。
Make sure .py
files are associated with the Python launcher C:\\Windows\\py.exe
or directly with eg 'C:\\Python27\\python.exe then edit your
PATHEXT environment variable using (System Properties) to add
;.PY` at the end.确保
.py
文件与 Python 启动器C:\\Windows\\py.exe
相关联或直接与例如“C:\\Python27\\python.exe”相关联, then edit your
environment variable using (System Properties) to add
then edit your
PATHEXT environment variable using (System Properties) to add
;.PY` 在结束。 You can now launch Python files in the current directory by typing their name.您现在可以通过键入名称来启动当前目录中的 Python 文件。
To be able to launch a given Python script from any directory, you can either put it in a directory that's already on the PATH
, or add a new directory to PATH
(I like creating a bin
directory in my user folder and adding %USERPROFILE%\\bin
to PATH) and put it there.为了能够从任何目录启动给定的 Python 脚本,您可以将它放在一个已经在
PATH
上的目录中,或者向PATH
添加一个新目录(我喜欢在我的用户文件夹中创建一个bin
目录并添加%USERPROFILE%\\bin
到 PATH)并将其放在那里。
Note that this is more a "how do I use Windows" question rather than a Python question.请注意,这更像是一个“我如何使用 Windows”的问题,而不是一个 Python 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.