如何从Windows命令行运行python文件(.py),而不必先键入python? [英] How to run a python file (.py) from the windows command-line without having to type python first?

查看:579
本文介绍了如何从Windows命令行运行python文件(.py),而不必先键入python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为file.py的python文件。
通常从命令行运行这个文件我会这样做:

Suppose I have a python file named file.py. Normally to run this file from the command-line I would do:

python path\to\file\file.py

我的问题是,是否可以这样做, $ c> python 之前的文件路径如下:

My question is, is it possible to do this without having the python before the file path like so:

path\to\file\file.py

或者,如果我在环境变量中有file.py的路径,只是:

Or, if I have the path to file.py in my Environment Variables, simply just:

file.py

我想,值得注意的是,我想使用一个python文件来接受命令行参数。感谢:)

I suppose it's worth noting I want to do this with a python file that is going to accept command-line arguments. Thanks :)

推荐答案

你面临的问题是你的python应用程序实际上不是一个应用程序。它是一个解释性脚本。这是因为Python是翻译语言

The problem that you are facing is the fact that your python application is not actually an application. It is an interpreted script. This is because Python is an Interpreted Language.

这与你有一个Word或Excel文档类似。这些是由他们的应用程序:Word和Excel,分别解释。操作系统知道使用注册的相关程序解释它们的应用程序。

This would be similar to you have a Word or Excel document. These are interpreted by their applications: Word and Excel, respectively. The operating system knows what application to use to interpret them using the registered associated programs.

官方Python常见问题解答在这里解释:
https://docs.python.org/2/faq /windows.html#how-do-i-make-python-scripts-executable

The official Python FAQs explains this here: https://docs.python.org/2/faq/windows.html#how-do-i-make-python-scripts-executable

这篇关于如何从Windows命令行运行python文件(.py),而不必先键入python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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