[英]Running python scripts on windows 10 cmd just by typing python nameofthescript.py
I can't run python scripts just by typing python nameofthescript.py
on Windows 10 cmd
.我无法通过在 Windows 10
cmd
上键入python nameofthescript.py
来运行 python 脚本。 The only way to make it work is by typing python
followed by space and C:\script\directory\nameofthefile.py
.使其工作的唯一方法是键入
python
后跟空格和C:\script\directory\nameofthefile.py
。
I just would like to use a quick way to run python scripts.我只想使用一种快速的方式来运行 python 脚本。 Already tried to type
py
instead of python.已经尝试输入
py
而不是 python。
I've already added the directory to the PATH
environment variable.我已经将该目录添加到
PATH
环境变量中。 Have not been able to find the answer online.一直没能在网上找到答案。 If more information is needed to solve the problem, I can try to provide it.
如果需要更多信息来解决问题,我可以尝试提供。
dpath
is the equivalent to path
for data files. dpath
相当于数据文件的path
。 Type dpath /?
dpath /?
note it was once called append
and the help hasn't been updated.请注意,它曾经被称为
append
并且帮助尚未更新。
That answers your question as you've asked.正如您所问的那样,这回答了您的问题。
Perhaps you should have asked how do I just type my script name ?也许您应该问我如何只输入我的脚本名称?
So所以
associate .py
with the python interpreter.将
.py
与 python 解释器相关联。
Use commands ftype
and assoc
使用命令
ftype
和assoc
ftype PythonScript=c:\pathtofolder\python.exe %*
assoc .py=PythonScript
Then add your SCRIPT path to the path command然后将您的脚本路径添加到路径命令
Setx path "%path%;C:\MyPythonScriptFolder"
Set path=%path%;C:\MyPythonScriptFolder"
Then tell windows command prompt to assume.py files are executable so it will search the path for it.然后告诉 windows 命令提示符 assume.py 文件是可执行的,因此它将搜索它的路径。
Set pathext=%pathext%;.py
Your command prompt should be in the folder with the file in it.您的命令提示符应该位于包含该文件的文件夹中。 Eg, if you
cd C:\script\directory
then you could python nameofthescript.py
例如,如果你
cd C:\script\directory
那么你可以python nameofthescript.py
Check if your file has permission to be executed.检查您的文件是否具有执行权限。 I'm not familiar with Windows, but on Linux it's
chmod +x name_of_file.py
, or with sudo, if you need root privileges.我不熟悉 Windows,但在 Linux 上它是
chmod +x name_of_file.py
,或者如果您需要 root 权限,则使用 sudo。 Also, check your python default version, because in most cases python calls python2.7 and not python3...另外,检查您的 python 默认版本,因为在大多数情况下 python 调用 python2.7 而不是 python3 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.