Home > database >  How to manage Python path on MAC
How to manage Python path on MAC

Time:03-01

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

CodePudding user response:

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

CodePudding user response:

you should use pyenv to isolate environment between each project

  • Related