在Windows上将Python设置为不键入"python"在cmd中 [英] Set up Python on Windows to not type "python" in cmd
问题描述
如何配置以便不必在Windows的CMD中键入python script.py
而只需键入script.py
?
How do I have to configure so that I don't have to type python script.py
but simply script.py
in CMD on Windows?
我将python目录添加到包含python.exe的%PATH%中,但是脚本仍然无法正确运行.
I added my python directory to %PATH% that contains python.exe but still scripts are not run correctly.
我用django-admin.py尝试过.运行django-admin.py startproject mysite
给我
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
在它前面使用python可以正确处理命令.
I tried it with django-admin.py Running django-admin.py startproject mysite
gives me
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Using python in front of it processes the command correctly.
这是什么问题?
推荐答案
C:\> assoc .py=Python
C:\> ftype Python="C:\python27\python.exe %1 %*"
或者相关路径是什么-您还可以使用ftype设置命令行参数.
Or whatever the relevant path is - you can also set command line args using ftype.
要在不给出后缀(.py
)的情况下识别命令,类似于对.exe
文件的工作方式,请将.py
添加到(全局)<用分号分隔的列表中PATHEXT
变量.
In order to make a command recognized without having to give the suffix (.py
), similar to how it works for .exe
files, add .py
to the semi-colon separated list of the (global) PATHEXT
variable.
ETA 2017-07-27
似乎这样的话题仍然引起人们的关注,想要为Win10用户(从@shadowrunner)提出有用的评论:
Seems like this is still getting eyeballs, wanted to elevate a useful comment for Win10 users (from @shadowrunner):
对于我来说,要使其在Win10下工作,实际的命令是(请注意引号的位置):
C:\> ftype Python="c:\Anaconda2\python.exe" "%1" %*
ETA 2019-02-01
谈论常绿!
首先,如果您是新安装Python,我强烈建议您查看@NunoAndré的答案.
First of all, if you're newly installing Python, I highly recommend reviewing the answer by @NunoAndré .
第二,为了澄清最近评论中的某些内容,请注意:您必须同时做两个部分(assoc
和ftype
),或者在ftype
命令中使用预先存在的关联标签.
Secondly, to clarify something from a recent comment, please note: you must do both parts (assoc
and ftype
), or use a pre-existing association label in the ftype
command.
默认情况下,至少对于Windows 8.1下的Python 3.7,.py
的关联为Python.File
,因此,除非首先更改关联,否则执行我上面编写的ftype
命令将无法正常工作.或者,您可以只使用ftype
并提供默认关联.由你决定.
By default, at least for Python 3.7 under Windows 8.1, the association for .py
is Python.File
, so performing the ftype
command I wrote above will not work correctly unless the association is first changed. Or you can just use ftype
and give the default association instead. Up to you.
这篇关于在Windows上将Python设置为不键入"python"在cmd中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!