Python 尝试启动应用程序时无法导入 Django 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39282309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:04:29  来源:igfitidea点击:

Couldn't import Django error when I try to startapp

pythondjangopython-3.x

提问by user3088202

I usually work on PC's but started to work on projects on my mac. I run Python 3 and when I started a new project I did the following:

我通常在 PC 上工作,但开始在我的 mac 上处理项目。我运行 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. Below is what was in the terminal:

当我尝试 python3 manage.py startapp www 时,我收到一个错误,指出 Django 无法导入。以下是终端中的内容:

(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?

回答by Sudaraka

Try using

尝试使用

python -m pip install django

This is the safest way as far as I know

据我所知,这是最安全的方式

回答by Admiral Giggles

I had the same problem, make sure you activated virtualenv, since once you close cmd it is no longer activated:

我遇到了同样的问题,请确保您激活了 virtualenv,因为一旦您关闭 cmd 它就不再被激活:

env\Scripts\activatein cmd

env\Scripts\activate在 cmd 中

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

回答by user11229780

I also had the same problem when I use Pycharm. I solved it by adding:

我在使用 Pycharm 时也遇到了同样的问题。我通过添加解决了它:

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

to manage.py

管理.py