不能在virtualenv中使用Django

时间:2017-10-09 16:46:58

标签: python django virtualenv

我有两个Django项目,我为他们创建了两个不同的virtualenv。 当我创建另一个virtualenv并安装Django并创建一个django项目时,我尝试了python manage.py runserver并出现此错误:

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    import django
ImportError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

我需要做什么? 我已经尝试卸载Django,pip,virtualenv并重新安装:

sudo apt-get install python3-pip
sudo pip3 install virtualenv
sudo virtualenv ENV
source newenv/bin/activate
sudo -H pip3 install django

1 个答案:

答案 0 :(得分:1)

sudo与virtualenvs一起使用可能会导致很多范围问题,以及virtualenvs的优点是你不需要root权限(在大多数情况下)。

此外,如果你也为python 2安装了virtaulenv,它可能默认为那个。

sudo apt-get install python3-pip
sudo pip3 install virtualenv

# I prefer using this over `virtualenv --python=/usr/bin/python3 ENV`
python3 -m venv ENV 
source ENV/bin/activate

# Can do a `which pip3` here to make sure it's using the ENV one
pip3 install django

# Could also do full path of `ENV/bin/pip3 install django`