我的问题是让 manage.py syncdb 在 virtualenv 中运行。
它一度运行良好,但似乎在我安装 South 并更新 pip 和分发时坏了。
无论如何,当 virtualenv 被激活时,我可以在交互式解释器中很好地导入应用程序。通过 mod_wsgi 运行,应用程序也被导入,并且站点可以运行。
当我运行 manage.py syncdb 时,它无法在我的 virtualenv 中的 INSTALLED_APPS 中找到任何应用程序。它可以很好地获取系统安装的应用程序,但在尝试导入仅限 virtualenv 的应用程序时失败。
最佳答案
嗨,这是一个老问题,但看到它没有回答。不确定您要做什么,但基本上有两种模式可以使用 virtualenv,
在第一种情况下,您需要先使用源 venv/bin/activate 激活您的 virtualenv,因为在部署时,您需要确保为您的网站代码激活了 virtualenv。我个人更喜欢以下方法来确保您的路径设置正确。 (我在做开发的时候也把这个加到我的manage.py里,这样我就不用担心先激活环境了。
修改了manage.py
#!/usr/bin/env python
import os.path
# Cater for Virtual env, add to sys.path
pwd = os.path.abspath(os.path.dirname(__file__))
project = os.path.basename(pwd)
new_path = pwd.strip(project)
activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)
这是有效的,由于我如何构建我的项目,您必须将其更改为您的目录结构。我的项目结构如下:
TopLevelDir
|
|- Project DIR
|- venv
|- requirements
|- deployment configs
关于使用 manage.py syncdb 时 Django 在 virtualenv 中找不到应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3560225/