简体   繁体   English

运行django-admin startproject myproject时没有名为django.core的模块

[英]No module named django.core when running django-admin startproject myproject

when running django-admin startproject myproject on macOS I get the error 在macOS上运行django-admin startproject myproject时出现错误

Traceback (most recent call last): 追溯(最近一次通话):
File "/usr/local/bin/django-admin", line 2, in 文件“ / usr / local / bin / django-admin”,第2行,在
from django.core import management 从django.core导入管理
ImportError: No module named django.core ImportError:没有名为django.core的模块

I checked out this question but running import django won't produce any output in a python3 shell. 我检查了这个问题,但运行import django不会在python3 shell中产生任何输出。

/usr/local/bin/django-admin is a symlink to /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/bin/django-admin.py . /usr/local/bin/django-admin/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/bin/django-admin.py的符号链接。

I already put /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django in my PYTHONPATH as suggested in other questions. 我已经按照其他问题的建议将/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django放在我的PYTHONPATH中。

Am I missing something? 我想念什么吗?

Even if you make it work, it is not good practice to do what you're doing! 即使您使它工作,也不是做您正在做的事的好习惯! Ideally, the only python-related binaries you would want in /usr/local/bin/ would be python, pip and virtualenv (or venv, pyvenv)... 理想情况下,/ usr / local / bin /中唯一想要的与python相关的二进制文件是python,pip和virtualenv(或venv,pyvenv)...

I would suggest you to delete /Library/Frameworks/Python.framework/Versions/3.6 ONLY IF you installed it there. 我建议您仅在安装了/Library/Frameworks/Python.framework/Versions/3.6下删除它。 As far as I know, macOS only comes with python2.7 installed and not python3.6! 据我所知,macOS仅安装了python2.7而不是python3.6!

Then open a new shell and try this: 然后打开一个新的外壳并尝试以下操作:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install python3
pip3 install virtualenv
cd ~/Desktop/
mkdir proj
cd proj
virtualenv -p python3 env
source env/bin/activate
pip install django
django-admin.py startproject testproj

skip the first step if you already have brew installed 如果您已经安装了brew,请跳过第一步

Check your permissions in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ with an ls -la command. 使用ls -la命令在/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/检查您的权限。 If you see anything owned by root , this probably needs to change. 如果看到root拥有的任何东西,则可能需要更改。 I suspect that since you installed some packages as root, the permissions are weird and it can't find the module. 我怀疑由于您以root用户身份安装了某些软件包,因此权限很奇怪,并且找不到模块。 If this is the case, reinstall the package(s) as your own user using sudo . 如果是这种情况,请使用sudo以您自己的用户身份重新安装软件包。

Another thing you should check: as phd mentioned you need to make sure you're using the version of python you think you are. 您应该检查的另一件事:如phd所述,您需要确保使用的是您认为的python版本。 Check this by running which python to tell you the location of the one you're referencing, and python --version to tell you which version you're using. 通过运行which python来告诉您所引用的python --version的位置,并通过python --version来进行检查,以告诉您所使用的版本。 If it's not Python 3.6, then you installed Django for a different version of Python. 如果不是Python 3.6,则为其他版本的Python安装了Django。 In this case, simply install Django for version 3.6 and you'll be on your way. 在这种情况下,只需安装适用于3.6版的Django,您就会步入正轨。

For future reference, Python offers a module called venv to prevent version mishaps like this. 为了将来参考,Python提供了一个名为venv的模块,以防止发生这种情况。 More info can be found here . 更多信息可以在这里找到。

It seems like you are trying to create or work on a django project without using a python virtual environment. 似乎您正在尝试在不使用python虚拟环境的情况下创建或处理django项目。 I recommend reviewing the python 3 venv documenation ( https://docs.python.org/3/library/venv.html ). 我建议您回顾python 3 venv文档( https://docs.python.org/3/library/venv.html )。 Then creating a virtual environment (venv) specifically for your web application. 然后为您的Web应用程序创建一个虚拟环境(venv)。 Once you you that project's venv setup you can install django into that venv. 一旦您完成了该项目的venv设置,就可以将django安装到该venv中。

在MacOS上,在命令前使用sudo

sudo django-admin startproject myproject

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

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