简体   繁体   English

如何将 python 脚本添加到 Windows 系统路径?

[英]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.

相关问题 如何使用 python 添加 windows 系统路径? - how to add windows system path with python? 如何添加默认路径来查找python脚本文件? - How can I add a default path to look for python script files in? 如何在python脚本中修改系统路径变量? - how do I modify the system path variable in python script? 如何使Python脚本成为系统命令并在Windows中的任何目录中轻松运行? - How can I make my Python script a system command and run it easily in any directory in Windows? 如何使用 WMI -(Windows Management Instrumentation)从 python 脚本获取远程系统硬件信息 - How can I get remote system hardware information using WMI -( Windows Management Instrumentation) from python script 如何在Windows上启动python脚本? - How can I launch a python script on windows? 如何在Windows上运行python脚本? - How can I run a python script on windows? 在Windows下自动将Python路径添加到PATH系统变量 - add Python path to PATH system variable automatically under Windows 执行 python 项目中的脚本时,如何向 $PYTHONPATH 或 $PATH 变量添加路径? - How can I add paths to the $PYTHONPATH or $PATH variables when a script within my python project is executed? 如何在Mac上更改Python的系统路径? - How can I change my system path for Python on a Mac?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM