导入错误:没有名为django的模块

时间:2022-06-05 18:09:40

I am using centos linux.

我正在使用centos linux。

I had python 2.6 with django and now i upgraded to python 2.7.
Python 2.6 is located in /usr/lib/python2.6.
Python 2.7 is located in /usr/local/lib/python2.7.
They both have site-packages directory and they both contain django 1.2.

我有django的python 2.6,现在我升级到python 2.7。 Python 2.6位于/usr/lib/python2.6中。 Python 2.7位于/usr/local/lib/python2.7中。它们都有site-packages目录,它们都包含django 1.2。

If i run python i get the 2.7 version.
My problem is that if try to import django i get

如果我运行python我得到2.7版本。我的问题是如果尝试导入django我得到

ImportError: No module named django

ImportError:没有名为django的模块

I am not sure where is my PYTHONPATH defined and if this is what i need to change. anyone ?

我不确定我的PYTHONPATH在哪里定义,如果这是我需要改变的。任何人 ?

i ended up making a symbolic link to the 2.6 site-packages directory.

我最终建立了2.6 site-packages目录的符号链接。

5 个解决方案

#1


13  

To check your path, you can use the following code:

要检查路径,可以使用以下代码:

import sys     
print(sys.path)

If you already know where django is installed, it should be easy to test if the desired directory is in your path with directory in sys.path.

如果您已经知道django的安装位置,那么应该很容易测试所需的目录是否在sys.path中的目录路径中。

Regarding where your PYTHONPATH is defined, note that it's an environment variable, so you can check its value (if defined) with: echo $PYTHONPATH

关于定义PYTHONPATH的位置,请注意它是一个环境变量,因此您可以使用以下命令检查其值(如果已定义):echo $ PYTHONPATH

#2


13  

Under linux, you can set the PYTHONPATH environment variable in your .profile or .bashrc. You can either edit it directly from the terminal by changing to your home directory (cd ~), and then edit the file (nano .bashrc), or by opening the file with gtkedit or vim or whatever, and add:

在linux下,您可以在.profile或.bashrc中设置PYTHONPATH环境变量。您可以通过更改到主目录(cd~)直接从终端编辑它,然后编辑文件(nano .bashrc),或者使用gtkedit或vim或其他任何内容打开文件,并添加:

PYTHONPATH=/usr/local/lib/python2.7/site-packages:/another/path/etc

If you want to test this before editing your profile, you can export this from the terminal as:

如果要在编辑配置文件之前对其进行测试,可以将其从终端导出为:

export PYTHONPATH=/local/lib/python2.7/site-packages

I'm assuming you're running this straight from the command line. If you're running it as a wsgi module in apache, you can add this to your syspath from your wsgi file as:

我假设你是从命令行直接运行的。如果您在apache中将其作为wsgi模块运行,则可以从wsgi文件中将其添加到syspath中:

import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')

#3


10  

try

尝试

pip freeze

this command show which packages are installed in your system then run with root privilege

此命令显示系统中安装了哪些软件包,然后以root权限运行

pip install django

then create a new project with command

然后用命令创建一个新项目

django-admin.py startproject mysite

then start your project

然后开始你的项目

cd path/to/mysite
./manage.py runserver 

in file wsgi.py add this lines

在文件wsgi.py中添加此行

import os
import sys
DJANGO_PATH =  os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)

#4


9  

Try printing sys.path to see what's in your path. Django need to be in one of the dirs listed. Example on Windows:

尝试打印sys.path以查看路径中的内容。 Django需要在列出的其中一个目录中。 Windows上的示例:

>>> import sys
>>> for p in sys.path: print p

C:\Python27\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
>>> 

#5


7  

I had the same error, and this fix my issue

我有同样的错误,这解决了我的问题

python -m pip install django

python -m pip install django

:) Done!

:)完成!

#1


13  

To check your path, you can use the following code:

要检查路径,可以使用以下代码:

import sys     
print(sys.path)

If you already know where django is installed, it should be easy to test if the desired directory is in your path with directory in sys.path.

如果您已经知道django的安装位置,那么应该很容易测试所需的目录是否在sys.path中的目录路径中。

Regarding where your PYTHONPATH is defined, note that it's an environment variable, so you can check its value (if defined) with: echo $PYTHONPATH

关于定义PYTHONPATH的位置,请注意它是一个环境变量,因此您可以使用以下命令检查其值(如果已定义):echo $ PYTHONPATH

#2


13  

Under linux, you can set the PYTHONPATH environment variable in your .profile or .bashrc. You can either edit it directly from the terminal by changing to your home directory (cd ~), and then edit the file (nano .bashrc), or by opening the file with gtkedit or vim or whatever, and add:

在linux下,您可以在.profile或.bashrc中设置PYTHONPATH环境变量。您可以通过更改到主目录(cd~)直接从终端编辑它,然后编辑文件(nano .bashrc),或者使用gtkedit或vim或其他任何内容打开文件,并添加:

PYTHONPATH=/usr/local/lib/python2.7/site-packages:/another/path/etc

If you want to test this before editing your profile, you can export this from the terminal as:

如果要在编辑配置文件之前对其进行测试,可以将其从终端导出为:

export PYTHONPATH=/local/lib/python2.7/site-packages

I'm assuming you're running this straight from the command line. If you're running it as a wsgi module in apache, you can add this to your syspath from your wsgi file as:

我假设你是从命令行直接运行的。如果您在apache中将其作为wsgi模块运行,则可以从wsgi文件中将其添加到syspath中:

import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')

#3


10  

try

尝试

pip freeze

this command show which packages are installed in your system then run with root privilege

此命令显示系统中安装了哪些软件包,然后以root权限运行

pip install django

then create a new project with command

然后用命令创建一个新项目

django-admin.py startproject mysite

then start your project

然后开始你的项目

cd path/to/mysite
./manage.py runserver 

in file wsgi.py add this lines

在文件wsgi.py中添加此行

import os
import sys
DJANGO_PATH =  os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)

#4


9  

Try printing sys.path to see what's in your path. Django need to be in one of the dirs listed. Example on Windows:

尝试打印sys.path以查看路径中的内容。 Django需要在列出的其中一个目录中。 Windows上的示例:

>>> import sys
>>> for p in sys.path: print p

C:\Python27\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
>>> 

#5


7  

I had the same error, and this fix my issue

我有同样的错误,这解决了我的问题

python -m pip install django

python -m pip install django

:) Done!

:)完成!