Python 没有名为 django 的模块,但已安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35184458/
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 16:06:23  来源:igfitidea点击:

No module named django but it is installed

pythondjangoubuntupythonpath

提问by srk

I have two versions of python 2.7 and 3.4 and installed django through pip. it shows in ubuntu terminal:

我有两个版本的 python 2.7 和 3.4,并通过 pip 安装了 django。它在 ubuntu 终端中显示:

$ pip freeze
Django==1.6.11
$ pip --version
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)
$ python
Python 2.7.9 (default, Feb  3 2016, 02:50:32) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import django
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named django
>>> import sys
>>> sys.path
['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']
>>> 

Any idea??

任何的想法??

回答by awesoon

Probably, pipinstalls packages into dist-packagesdirectory, which is not included into PYTHONPATHenvironment variable. You have a couple of solutions:

可能pip将软件包安装到dist-packages目录中,该目录未包含在PYTHONPATH环境变量中。你有几个解决方案:

  1. Create and configure virtualenvfor your project, before using pip. This is the most Pythonic way
  2. Try to install Djangousing built-in pip module:

    python -m pip install django
    

    This command should install packages into site-packagesdirectory.

  3. You may also add dist-packagesto your PYTHONPATH. This question should help you: How to globally modify the default PYTHONPATH (sys.path)?
  1. 创建和配置virtualenv使用前为您的项目,pip。这是最 Pythonic 的方式
  2. 尝试Django使用内置 pip 模块安装:

    python -m pip install django
    

    此命令应将软件包安装到site-packages目录中。

  3. 您也可以添加dist-packages到您的PYTHONPATH. 这个问题应该对你帮助:如何全局修改默认的 PYTHONPATH (sys.path)?

回答by DevB2F

I got this error when using

使用时出现此错误

python manage.py runserver #python version 3 was being used

Solved the problem by using:

使用以下方法解决了问题:

python2 manage.py runserver #python version 2

回答by cjones

For Mac users; If you've previously downloaded and installed python3, just running pythonvia the terminal shell defaults to using the pre-installed python v2, which will not recognise your installation of django (unless you install it via the python2 module) and you'll probably get an error when you check the version:

对于 Mac 用户;如果您之前已经下载并安装了 python3,仅python通过终端 shell运行默认使用预装的 python v2,它不会识别您安装的 django(除非您通过 python2 模块安装它)并且您可能会得到检查版本时出错:

$ python

>>> from django import get_version
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named django

Try starting python using:

尝试使用以下命令启动 python:

$ python3

Then try:

然后尝试:

>>> from django import get_version
>>> get_version()

You should get the output:

你应该得到输出:

'2.0.3'

回答by Pransh Tiwari

I had given the dependency in requirements.txtas Django==2.0.7. So after activating the virtual environment, if you are receiving the message, try installing the requirements:

我已经在requirements.txtas 中给出了依赖Django==2.0.7。所以激活虚拟环境后,如果收到消息,尝试安装需求:

pip install -r requirements.txt

回答by WeirdMachine

You need to close the other active virtual environment in your machine if you're using virtualenv, and make sure django installed. Go to python3 interpreter and run this:

如果您使用的是 virtualenv,则需要关闭机器中的其他活动虚拟环境,并确保已安装 django。转到python3解释器并运行:

>>>from django import get_version
>>>get_version()

make sure it show you this '2.1.4'

确保它显示你这个'2.1.4'

回答by Blue Sky

I faced same issue with you when try to setup Apache work with Django. Issue solve after add Pythonpath to Apache2.conf as bellow:

在尝试使用 Django 设置 Apache 工作时,我遇到了同样的问题。将 Pythonpath 添加到 Apache2.conf 后问题解决如下:

WSGIPythonPath/opt/djangoprojects/myproject:opt/anaconda3/lib/python3.6/site-packages

I installed Python3 before.

我之前安装过Python3。

回答by Roshan Yadav

I also faced the same problem. i was using python 3.7 and installed django 2.2. So i degraded my python to 3.6 and installed django 2.2, and without having a virtualenv.

我也面临同样的问题。我使用的是 python 3.7 并安装了 django 2.2。所以我将我的 python 降级到 3.6 并安装了 django 2.2,并且没有 virtualenv。

回答by user11751679

This error shows you didn't install Django. Installing Django should solve the problem.

此错误表明您没有安装 Django。安装 Django 应该可以解决问题。

Once you do, you can just check the path of "django" using:

完成后,您可以使用以下命令检查“django”的路径:

>>> sys.path

回答by Vishwas Navada K

I got the same issue today. doing pip3 install djangoor pip3 install -r requirements.txtsolved it.

我今天遇到了同样的问题。做 pip3 install djangopip3 install -r requirements.txt解决它。