简体   繁体   中英

Can't get Django to work on my Mac

I'm having difficulty getting Django to work on my Mac. I pip installed it, as well as downloading it on PyCharm. I have a feeling it will work on PyCharm if I knew what I'm doing. 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? Meaning, if you know the folders and content I think I could get it running on PyCharm. My PyCharm says it's been downloaded, so I think this would be a great way to go into it.

Many thanks.

Seems to me that pip is not configured with the python that you are using. From the output you posted, pip is installing Django for the python executable residing in here:

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

what is the output of ls -l $(which python) ?

This will tell you where the python you are using is. If it's different than the path above, pip is installing packages on another python executable.

You have 2 quick options.

  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.

    ln -s {target-filename} {symbolic-filename}

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
    source ./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 !

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 . This virtual environment should work and be less prone to other odd PATH problems.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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