简体   繁体   English

在命令提示符中运行python脚本的问题(特别是使用命令行参数)?

[英]Issues running python scripts in Command Prompt (Specifically with command line arguments)?

I am trying to run my python scripts in the command-prompt without calling python.exe first. 我试图在命令提示符下运行我的python脚本而不先调用python.exe。 I am specifically doing this in relation to running django-admin.py. 我特意这样做是为了运行django-admin.py。 I have C:\\Python26 and C:\\Python26\\Scripts in my PATH. 我的PATH中有C:\\ Python26和C:\\ Python26 \\ Scripts。 However, if I try running django-admin.py by doing: 但是,如果我尝试运行django-admin.py:

django-admin.py startproject helloworld django-admin.py startproject helloworld

I get the message: Type 'django-admin.py help' for usage. 我收到消息:键入'django-admin.py help'以供使用。

Now, after some experimentation, I realized the problem is that the secondary arguments to these scripts are not being passed for some reason, since I tried it with a some other python scripts I have. 现在,经过一些实验,我意识到问题是这些脚本的辅助参数由于某种原因没有被传递,因为我用其他一些python脚本尝试了它。 I know I could avoid this problem by simply doing: 我知道我可以通过以下方式避免这个问题:

python C:\\Python26\\Scripts\\django-admin.py startproject helloworld python C:\\ Python26 \\ Scripts \\ django-admin.py startproject helloworld

But I know it should be possible to run the first command only and get it to work, because I had it working before. 但我知道应该可以只运行第一个命令并让它工作,因为我之前有它工作。 I've looked everywhere, and not many places have been helpful so any idea would be useful for me at this point. 我到处寻找,并没有多少地方有所帮助,所以任何想法对我来说都是有用的。

Update: The .py file associations were set correctly, and the problem is still occuring. 更新:.py文件关联设置正确,问题仍然存在。

Check assoc and ftype . 检查assocftype If properly set, you can run a .py with arguments. 如果设置正确,您可以使用参数运行.py

> assoc .py
.py=Python.File
> ftype Python.File
Python.File="C:\Python26\python.exe" "%1" %*

Depending on how your Python was installed, these may or may not be in place. 根据Python的安装方式,这些可能会也可能不会到位。 You can set them with assoc and ftype . 您可以使用assocftype 设置它们。

> assoc .py=Python.File
> ftype Python.File="C:\Python26\python.exe" "%1" %*

Also, if .py is included in the PATHEXT environment variable, you can run .py files without the trailing .py . 此外,如果.py包含在PATHEXT环境变量中,则可以运行.py文件而不使用尾随.py

> set PATHEXT=%PATHEXT%;.py
> django-admin startproject helloworld

I know this is an old thread, but I have searched for a few weeks for this very same problem and found nothing. 我知道这是一个老线程,但是我已经搜索了几个星期来解决同样的问题但没有发现任何问题。 Today, however, I tried something new: 然而今天,我尝试了一些新的东西:

If you are using Windows 7, do not use Command Prompt for scripting purposes. 如果您使用的是Windows 7,请不要使用命令提示符进行脚本编写。

Instead, use the Windows PowerShell located at: All Programs -> Accessories -> Windows PowerShell -> Windows PowerShell . 而是使用位于以下位置的Windows PowerShell: All Programs -> Accessories -> Windows PowerShell -> Windows PowerShell In there you can run the command django-admin.py startproject mysite if you added the correct paths to your environmental paths. 在那里,如果向环境路径添加了正确的路径,则可以运行命令django-admin.py startproject mysite

BTW, I'm now using Python 2.7 with Django 1.2.4 on Windows 7 Ultimate 32bit. 顺便说一下,我现在在Windows 7 Ultimate 32bit上使用带有Django 1.2.4的Python 2.7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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