'python -m django' 在 vi​​rtualenv 中给出了冲突的信息

'python -m django' gives conflicting messages in virtualenv

我创建了一个虚拟环境:

virtualenv --python=/usr/local/bin/python2.7 workoutpy2.7 

我正在开发一个锻炼日记网络应用程序,因此得名。 然后我在里面安装了django nonrel:

pip install git+https://github.com/django-nonrel/django@nonrel-1.5

然后是 django nonrel 的 djangotoolbox:

pip install git+https://github.com/django-nonrel/djangotoolbox

然后是 mongodb 引擎:

pip install git+https://github.com/django-nonrel/mongodb-engine

然后我假设 django 安装在我的 virtualenv 中。所以我输入:

python -m django

并得到以下输出:

/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workoutpy2.7/bin/python: No module named django.__main__; 'django' is a package and cannot be directly executed

这是什么意思?作为记录,我以这种方式安装的任何软件包都会发生同样的事情。看看这个:

(workoutpy2.7) Sahands-MacBook-Pro:workoutpy2.7 sahandzarrinkoub$ pip install pymongo
Requirement already satisfied: pymongo in ./lib/python2.7/site-packages
(workoutpy2.7) Sahands-MacBook-Pro:workoutpy2.7 sahandzarrinkoub$ python -m pymongo
/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workoutpy2.7/bin/python: No module named pymongo.__main__; 'pymongo' is a package and cannot be directly executed

感谢您的帮助。

python-m 选项允许您(尝试)运行 一个 module/package。来自文档:

-m <module-name>

Search sys.path for the named module and execute its contents as the __main__ module.

Since the argument is a module name, you must not give a file extension (.py). The module name should be a valid absolute Python module name, but the implementation may not always enforce this (e.g. it may allow you to use a name that includes a hyphen).

Package names (including namespace packages) are also permitted. When a package name is supplied instead of a normal module, the interpreter will execute <pkg>.__main__ as the main module. This behaviour is deliberately similar to the handling of directories and zipfiles that are passed to the interpreter as the script argument.

因此,-m 之后的内容需要通过定义 __main__ 来支持成为 运行,这就是为什么 python -m pymongo 然后声明“No module named pymongo.__main__” .


对于 Django 的情况,或者具体来说,django-nonrel:看起来 django-nonrel 已经好几年没有更新了,最近的分支看起来像是 Django 1.7 的一个分支。 Django proper 在 1.9 中添加了 django.__main__,所以这就是为什么您正在阅读的文档误导了您。您 可能 能够使用 Django 1.7 tutorial,但是 NoSQL 数据库可能会严重影响模型和查询,因此使用 nonrel 项目提供的教程(如果有的话)会优先考虑。