简体   繁体   中英

Running Python from the Windows Command Line

如何从Windows命令行( cmd.exe )运行Python文件,这样就不必每次都重新输入代码了?

Wouldn't you simply save your Python code into a file, and then execute that file using Python?

Save your code into a file called Test.py .

And then run it?

$ C:\Python24\Python.exe C:\Temp\Test.py

If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.

If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.

When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.

If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py") , import test (if that is a module), or finally from the debugger.

如果将Python可执行文件(python.exe)放在路径上,则可以使用python script.py调用脚本,其中script.py是要执行的Python文件。

A good tool to have is the IPython shell . Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.

You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables

通过按Win+R并在其中输入cmd来打开命令提示符,导航至脚本目录,然后编写: python script.py

在DOS中,您可以使用edit创建/修改文本文件,然后通过键入python [yourfile]执行它们

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM