简体   繁体   English

我尝试启动时无法导入Django错误

[英]Couldn't import Django error when I try to startapp

I usually work on PC's but started to work on projects on my mac. 我通常在PC上工作,但开始在我的Mac上处理项目。 I run Python 3 and when I started a new project I did the following: 我运行Python 3,当我开始一个新项目时,我做了以下事情:

1) In main project folder, installed virtualenv and activated it. 1)在主项目文件夹中,安装virtualenv并激活它。

2) Install Django and Gunicorn 2)安装Django和Gunicorn

3) Did startproject 3)启动项目

When I try to python3 manage.py startapp www I get an error that Django could not be imported. 当我尝试python3 manage.py startapp www时,我收到一个无法导入Django的错误。 Below is what was in the terminal: 以下是终端中的内容:

(venv) AB:directory AB$ pip freeze
Django==1.10
gunicorn==19.6.0

(venv) AB:directory AB$ ls
directory   manage.py

(venv) AB:directory AB$ python3 manage.py startpap www

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?

Try using 尝试使用

python -m pip install django

This is the safest way as far as I know 据我所知,这是最安全的方式

I had the same problem, make sure you activated virtualenv, since once you close cmd it is no longer activated: 我有同样的问题,请确保你激活virtualenv,因为一旦你关闭cmd它就不再被激活:

env\\Scripts\\activate in cmd 在cmd中使用env\\Scripts\\activate

Now cmd should have (env) just like this: (env) c:\\users\\user\\PROJECT\\.. 现在cmd应该有(env)就像这样: (env) c:\\users\\user\\PROJECT\\..

Now you can type: python manage.py runserver 现在您可以输入: python manage.py runserver

I also had the same problem when I use Pycharm. 当我使用Pycharm时,我也遇到了同样的问题。 I solved it by adding: 我通过添加以下内容解决了

import sys
  sys.path.append('/Users/et/PycharmProjects/Jieba_Analyzer/venv/lib/python3.6/site-packages')

to manage.py to manage.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM