1

I try to run Django project and create a new app. When the terminal run python manage.py makemigrations ,Django cannot be found and import error ,and the virtual environment is not activated?

After I tried to search the answer, it might be the problem of the interpreter. Then I found that I had installed multiple Python versions and anaconda.And macOS contains Python 2.7. I was completely confused.

The following is my zsh file


export PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8:$PATH"

#python
alias python='/usr/local/bin/python3' or alias python='/usr/bin/python3'

#conda
export PATH=/Users/smart/opt/anaconda3/bin:$PATH

#ruby

iterm2

 smart@smartMBP  ~  which python
python: aliased to /usr/bin/python3

 smart@smartMBP  ~  which -a python
python: aliased to /usr/bin/python3
/Users/smart/opt/anaconda3/bin/python
/usr/bin/python

 smart@smartMBP  ~  which -a python3
/Users/smart/opt/anaconda3/bin/python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
/usr/local/bin/python3
/usr/bin/python3

smart@smartMBP  ~  conda --version
conda 4.7.12

 smart@smartMBP  ~  echo $PATH
/Users/smart/opt/anaconda3/bin
/Users/smart/bin
/opt/flutter/bin
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
/Library/Frameworks/Python.framework/Versions/3.9/bin
/Library/Frameworks/Python.framework/Versions/3.8/bin

What shall I do? The path should use '/Library/Frameworks/Python.framework' or '/usr/bin/' ? Will Zsh only read the first effective path, or will it continue to read the overwritten path?

I want to delete and reinstall, but I'm afraid it will be bad

Thanks for any suggestions

5
  • Be careful changing the python path on mac, as mac still ships with python2 for the OS. Instead of running python manage.py makemigrations can you run python3 manage.py makemigrations? Commented Mar 1, 2022 at 2:53
  • @FreddyMcloughlan 'ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?' Commented Mar 1, 2022 at 3:00
  • 1
    Did you install Django on the correct python installation? pip3 install django or python3 -m pip install django Commented Mar 1, 2022 at 3:04
  • 1
    More specifically, try pip3.10 / pip3.x if you have multiple python versions Commented Mar 1, 2022 at 3:15
  • No worries man. pip may be pointing towards your other python installation (maybe the PATH one, not sure). Commented Mar 1, 2022 at 3:25

2 Answers 2

1

Use pip3.x (e.g pip3.10) to install the package on the latest/currently used version of Python:

pip3.10 install django

or

python3.10 -m pip install django

And make sure the program is run by the same python executable

python3.10 manage.py makemigrations
Sign up to request clarification or add additional context in comments.

1 Comment

For future visitors: Mac has python2 installed with the OS. so pip or python will use the Mac's version of python instead of your own (in most cases). So use the 3 suffix on pip and python to point towards your own [hopefully newer] installations. E.g. python3 main.py or pip3.10 install requests
0

you should use pyenv to isolate environment between each project

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.