简体   繁体   English

无法让Django在我的Mac上运行

[英]Can't get Django to work on my Mac

I'm having difficulty getting Django to work on my Mac. 我很难让Django在Mac上运行。 I pip installed it, as well as downloading it on PyCharm. 我点了安装它,并在PyCharm上下载了它。 I have a feeling it will work on PyCharm if I knew what I'm doing. 如果我知道自己在做什么,我有种感觉可以在PyCharm上使用。 I dont. 我不。 haha. 哈哈。 It's my first time. 这是我第一次 I am trying to work off the tutorial that they provide on their site. 我正在尝试完成他们在其站点上提供的教程。 Here is where I run into trouble. 这是我遇到麻烦的地方。

In terminal I type: 在终端我输入:

python -m django --version

and I get: 我得到:

/usr/bin/python: No module named django

but when I type: 但是当我键入:

pip install Django

I get: 我得到:

Requirement already satisfied (use --upgrade to upgrade): Django in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

The tutorial wants me to type: 本教程要我键入:

django-admin startproject mysite

and I get this: 我得到这个:

-bash: django-admin: command not found

So to my question. 所以对我的问题。 What is going on here? 这里发生了什么? I'm thinking my path to it is different than what is expected, though I'm not fully sure. 尽管我不确定,但我认为我的做法与预期不同。 If you know the startproject mysite could you give it to me? 如果您知道startproject mysite,可以给我吗? Meaning, if you know the folders and content I think I could get it running on PyCharm. 意思是,如果您知道文件夹和内容,我想我可以在PyCharm上运行它。 My PyCharm says it's been downloaded, so I think this would be a great way to go into it. 我的PyCharm说它已经下载了,所以我认为这是进入它的好方法。

Many thanks. 非常感谢。

Seems to me that pip is not configured with the python that you are using. 在我看来,pip未配置您正在使用的python。 From the output you posted, pip is installing Django for the python executable residing in here: 从您发布的输出中,pip正在为驻留在此处的python可执行文件安装Django:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

what is the output of ls -l $(which python) ? ls -l $(which python)的输出是什么?

This will tell you where the python you are using is. 这将告诉您正在使用的python在哪里。 If it's different than the path above, pip is installing packages on another python executable. 如果与上面的路径不同,则pip在另一个python可执行文件上安装软件包。

You have 2 quick options. 您有2个快速选择。

  1. Put a softlink to the python residing in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages inside your /usr/bin/ or wherever is imported before the path that which python shows. 将软链接放置到/usr/bin/中的/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages的python,或者将其导入到which python显示的路径之前的任何位置。

    ln -s {target-filename} {symbolic-filename} ln -s {目标文件名} {符号文件名}

which is probably 这可能是

ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/bin/python
  1. Use a Virtual Environment . 使用虚拟环境

    pyenv ./env pyenv ./env
    source ./env/bin/activate 源./env/bin/activate

Now you are working on a virtual environment, which has its own pip and python so you should be fine to do anything you'd want ! 现在,您正在虚拟环境中工作,它具有自己的pip和python,因此您可以做任何想做的事情!

Hope it helps 希望能帮助到你

From Comments 来自评论

Looks like a virtual environment problem. 看起来像是虚拟环境问题。 Without going into the extreme details of your environment settings, try the following: make and navigate to an empty directory, type pyvenv-3.5 ./Env , then source ./Env/bin/activate , next pip install django , and finally python -m django --version . 没有进入的环境设置了极致的细节,请尝试以下操作:拨打和导航到一个空目录,键入pyvenv-3.5 ./Env ,那么source ./Env/bin/activate ,未来pip install django ,最终python -m django --version This virtual environment should work and be less prone to other odd PATH problems. 该虚拟环境应该可以正常工作,并且不易出现其他奇怪的PATH问题。

Additional Info 附加信息

You will need to run the command source ./Env/bin/activate when you open up a new shell or run a bash script in order to active this environment. 打开新的Shell或运行bash脚本时,需要运行命令source ./Env/bin/activate ,以激活此环境。

Also, you can now manage your pip packages (including django) by using pip freeze > ./requirements.txt to create a lists of your packages w/ version numbers and 'pip install -r ./requirements.txt` to install the packages. 另外,您现在可以使用pip freeze > ./requirements.txt创建带有版本号的软件包列表,并使用'pip install pip freeze > ./requirements.txt requirements.txt`安装软件包来管理pip软件包(包括django)。 。

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

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