Python manage.py - 导入错误:没有名为 django 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24057015/
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
manage.py - ImportError: No module named django
提问by Tobias
I just ported a working django app from a windows system to ubuntu by just copying all the files to /var/www/some/dir/djangoApp
. But now, when executing
我只是通过将所有文件复制到/var/www/some/dir/djangoApp
. 但是现在,当执行
python manage.py runserver 8080
I get the Error:
我收到错误:
ImportError: no module named django
I have already installed a fresh version of django with python setup.py install
to /usr/local/lib/python2.7/dist-packages/django/
and added the path to PYTHONPATH.
我已经使用python setup.py install
to安装了新版本的 django/usr/local/lib/python2.7/dist-packages/django/
并将路径添加到 PYTHONPATH。
The linux system in not maintained by me and has numerous python versions installed.
linux 系统不是我维护的,并且安装了许多 python 版本。
calling >>> import django
in the shell does not raise an ImportError.
>>> import django
在 shell 中调用不会引发 ImportError。
I'm very confused. Please help me!
我很困惑。请帮我!
Here's the traceback from the console:
这是来自控制台的回溯:
Traceback (most recent call last):
File "manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 280, in execute
translation.activate('en-us')
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 130, in activate
return _trans.activate(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 188, in activate
_active.value = translation(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 159, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
ImportError: No module named django
采纳答案by asaji
Since you just migrated to a UNIX environment, i suggest you migrate also to the best practices on such a platform too.
由于您刚刚迁移到 UNIX 环境,我建议您也迁移到此类平台上的最佳实践。
Download PIP
sudo apt-get install python-pip
Download and install virtualenv to set up a separate python virtual environment for your apps. This will allow you to run different flavors of django and other software without conflicts.
sudo pip install virtualenv
Create virtual environment by running. You will get a folder called myvirtualenvironment with a bin folder and a few executables inside it.
virtualenv myvirtualenvironment --no-site-packages
In order to tell your shell that you're working with that newly created virtual environment you need to run the activate script found in
/myvirtualenvironment/bin/
source myvirtualenvironment/bin/activate
Now you can install django specifically to that virtual environment.
pip install django
ORpip install django==1.6
depending on what version you want to install. If you don't specify, the latest version will be installed.Now, migrate your Django project inside of
/myvirtualenvironment/
and run the the runserver command.
下载画中画
sudo apt-get install python-pip
下载并安装 virtualenv 为您的应用程序设置一个单独的 python 虚拟环境。这将允许您运行不同风格的 django 和其他软件而不会发生冲突。
sudo pip install virtualenv
通过运行创建虚拟环境。您将获得一个名为 myvirtualenvironment 的文件夹,其中包含一个 bin 文件夹和一些可执行文件。
virtualenv myvirtualenvironment --no-site-packages
为了告诉您的 shell 您正在使用新创建的虚拟环境,您需要运行在
/myvirtualenvironment/bin/
source myvirtualenvironment/bin/activate
现在您可以专门为该虚拟环境安装 django。
pip install django
或pip install django==1.6
取决于您要安装的版本。如果不指定,将安装最新版本。现在,将您的 Django 项目迁移到其中
/myvirtualenvironment/
并运行 runserver 命令。
回答by grouser
Sometimes there are some .pyc files in the directories and you don't get any error from the console. Trying to install Django from pip.
有时,目录中有一些 .pyc 文件,并且您不会从控制台收到任何错误。尝试从 pip 安装 Django。
sudo pip install django
Best practices advise to create a requirements.txt file (From you Windows installation)
最佳实践建议创建一个 requirements.txt 文件(来自您的 Windows 安装)
pip freeze > requirements.txt
And then create a new virutalenv to install every package
然后创建一个新的 virutalenv 来安装每个包
mkvirtualenv myapp
pip install -r requirements.txt
回答by Michael Romrell
I landed on this page after getting the same error (On a site I've been actively developing just fine for months). For me @asaji's answer reminded me that I had forgotten to launch my virtual env.
我在遇到同样的错误后登陆了这个页面(在一个我几个月来一直在积极开发的网站上)。对我来说,@asaji 的回答提醒我忘记启动我的虚拟环境。
After launching my Virtual Env . Scripts/activate
it worked great!
启动我的虚拟环境后,. Scripts/activate
它工作得很好!
回答by Supersvensk
It seems like quite a big job for a problem that (MIGHT) be very small.
对于(可能)非常小的问题来说,这似乎是一项相当大的工作。
I had this exact problem, it was working one day, and then the next day it wasn't working anymore. I'm pretty new to Linux and Django in general but knows python well so didn't really know where to look other than "Virtual environment"
我遇到了这个确切的问题,它在一天工作,然后第二天就不再工作了。总的来说,我对 Linux 和 Django 还是很陌生,但对 python 非常了解,所以除了“虚拟环境”之外,我真的不知道该去哪里找
I started installing virtual environments again (like some people suggest) BUT DON'T!
我再次开始安装虚拟环境(就像有些人建议的那样)但是不要!
At least not until you tried this and think about it:
至少在您尝试并考虑它之前不会:
Did you install your virtual environment as a temp one(did you perhaps install it like this: "pip install pipenv"?)
您是否将虚拟环境安装为临时环境(您是否可能像这样安装它:“ pip install pipenv”?)
if you did(like you should have done it) you will have somewhere around your current django project 2 files - pipfile& pipfile.lock
如果你做了(就像你应该做的那样),你将在当前的 django 项目 2 文件周围有一个地方 - pipfile& pipfile.lock
open your terminal, cd to the path of those files(same folder)
打开你的终端,cd到这些文件的路径(同一个文件夹)
write in terminal: pipenv shell
在终端中写入:pipenv shell
BOOM: You just reactivated your "TEMPORARY" virtual environment and Django works exactly as it should, will and did.
BOOM:您刚刚重新激活了“TEMPORARY”虚拟环境,Django 完全按照预期、意愿和做了。