成功执行Django命令的*唯一*方法是使用"python ...PATH...django-admin.py [选项]"。为什么不能简化?
这是对这个问题的后续讨论。
在Django教程的第一步中,建议你运行以下命令:
django-admin.py startproject mysite
但是结果是这样的:
R:\jeffy\programming\sandbox>django-admin.py startproject mysite
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on exception
--no-color Don't colorize the command output.
--version show program's version number and exit
-h, --help show this help message and exit
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
runserver
shell
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlinitialdata
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
R:\jeffy\programming\sandbox>
这和今天早些时候的情况大相径庭(之前有一个旧的Python版本藏在一个无关的应用程序的安装目录里——现在已经被删除,并且从PATH中移除了)。
这个命令也不行:
python django-admin.py startproject mysite
结果:
python: can't open file 'django-admin.py': [Errno 2] No such file or directory
这个命令也不行:
c:\applications\programming\python_341\Scripts\django-admin.py startproject mysite
回应:
R:\jeffy\programming\sandbox>c:\applications\programming\python_341\Scripts\django-admin.py startproject mysite
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on exception
--no-color Don't colorize the command output.
--version show program's version number and exit
-h, --help show this help message and exit
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
runserver
shell
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlinitialdata
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
但是这个可以:
python c:\applications\programming\python_341\Scripts\django-admin.py startproject mysite
我在遵循这个建议后,重复了所有这些命令:
把整个
django-admin.py
文件的内容注释掉,然后在顶部添加这行:import sys; print(sys.version, sys.executable)
命令:
django-admin.py startproject mysite
回应:
3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] C:\applications\programming\python_341\python.exe
命令:
python django-admin.py startproject mysite
回应:
python: can't open file 'django-admin.py': [Errno 2] No such file or directory
命令:
c:\applications\programming\python_341\Scripts\django-admin.py startproject mysite
回应:
3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] C:\applications\programming\python_341\python.exe
命令:
python c:\applications\programming\python_341\Scripts\django-admin.py startproject mysite
回应:
3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] C:\applications\programming\python_341\python.exe
作为额外的证据:
[R:\]python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
正如这里所建议的(感谢@Humdinger),这可能是一个PATH的问题。今天早些时候确实有一个旧版本的Python在一个不明显的目录里,我已经删除了它。
这是我的PATH:
C:\applications\programming\python_341\;
C:\applications\programming\python_341\Scripts;
C:\applications\programming\;
.;
C:\Program Files\Common Files\ArcSoft\Bin;
C:\Program Files\Common Files\Microsoft Shared\Windows Live;
C:\Program Files\Windows Live\Shared;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Windows\system32;
C:\applications\programming\apache-ant-1.8.1\bin;
C:\applications\programming\apache-maven-3.1.1\bin;
C:\applications\programming\jdk_7_51\bin;
C:\applications\video\quicktime\QTSystem\;
C:\Program Files\TortoiseSVN\bin;
%M2%;
C:\applications\utilities\gpg4win\pub
我检查了每一个目录,并执行了dir *python*
。唯一匹配的文件如下:
python34.dll
在C:\Windows\System32
(我不太明白这是什么)python.exe
和pythonw.exe
在C:\applications\programming\python_341
ipython
和ipython3
在C:\applications\programming\python_341\Scripts\
顺便说一下:Django版本1.7c2,Python版本3.1.4,Windows 7,32位。
有没有人能告诉我这是怎么回事?为什么这个命令不能以任何方式简化,而不导致错误?
2 个回答
我们遇到的问题很相似,但我能在不经过你们一半的排查步骤的情况下解决了。
结果发现,我在用户变量和系统变量中都有 ;C:\Python34\Scripts\
这个路径。我可能是无意中把这个路径加到了用户变量里,然后忘了删掉。
只需在你的用户变量中删除 ;C:\Python34\Scripts\
这个路径,问题就应该能解决了。
我在使用Django的时候也遇到了同样的问题,后来在这个链接找到了解决办法:https://docs.djangoproject.com/en/1.7/howto/windows/。里面提到,如果你使用的是Django 1.7,那么在命令提示符中要输入django-admin,而不是django-admin.py。希望这个信息对你有帮助。