嗨这是一个老问题,但看到它没有回答。不知道你想做什么,但基本上有两种模式可以使用 virtualenv,
- 为了发展,创造自给自足的环境
- 用于部署,创建自包含环境
在第一种情况下,您需要首先使用 source 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