在 Windows 上设置 Python 以不键入“python"在 cmd [英] Set up Python on Windows to not type "python" in cmd

查看:18
本文介绍了在 Windows 上设置 Python 以不键入“python"在 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 给了我 输入'django-admin.py help <子命令>'获取特定子命令的帮助. 在它前面使用 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:python27python.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.

预计到达时间 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:Anaconda2python.exe" "%1" %*

预计到达时间 2019-02-01

谈谈常青树!

首先,如果您是新安装 Python,我强烈建议您查看@NunoAndré 的答案.

First of all, if you're newly installing Python, I highly recommend reviewing the answer by @NunoAndré .

其次,为了澄清最近的评论中的某些内容,请注意:您必须同时完成这两个部分(assocftype),或者使用预先存在的关联标签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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆