2020.11.26
Beginner Python, a little knowledge of the environment configuration, this new computer configuration environment encountered some problems, so record the process and concept, convenient later review and for the majority of beginners reference, if there is a mistake, hope to correct. (The specific process is not described much, but only the pits encountered during the configuration and the significance of the operation.)
How to configure a Python virtual environment for a new Mac
Homebrew
Installation instructions:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(It seems that downloading is slow now, but you can find a connection of domestic mirror)- The installation
python3
Environment:brew install python3
- Install virtual environment:
sudo pip3 install virtualenv
- Install the add, delete, change, and check virtual environment extension package:
sudo pip3 install virtualenvwrapper
- Create a virtual environment directory:
mkdir ~/.virtualenvs
- Find the path to python3 and VirtualenvWrapper. sh:
which python3
andwhich virtualenvwrapper.sh
- Open the global configuration file:
open ~/.zshrc
- Configuring a global profile:
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bash_profile
Copy the code
- Compile to put the configuration into effect:
source ~/.zshrc
- Create a virtual environment:
mkvirtualenv -p python3 xxx
- Open the
Pycharm
You can choose a virtual environment for development!
Two. Personal understanding after stepping pit matters needing attention
- The original Mac comes with Python2. They usually use
HomeBrew
forpython2
andpython3
The purpose of the reinstallation is to recreate the environment used for development, isolated from the system environment. At presentHomeBrew
No longer searchablepython2
Python2 is no longer maintained and will be used in the futurepython3
Development, so the impact is not big, I use direct abandonpython2
Management of the environment. - Used when installing virtualenv and VirtualenvWrapper
sudo pip3 install
Instructions to install as the system defaultspython2
. mkdir ~/.virtualenvs
Directives are directories that hold multiple virtual environments,.
Is used to hide folders, make directory simple and easy to see, directory name can be named according to their own wishes, does not affect the use- use
which
Command to query the file directory and record it (which python3
andwhich virtualenvwrapper.sh
) - After the installation is complete, you need to configure the path because you need to specify the virtual environment directory and virtual environment directory
python
Version, each time using manual input is too cumbersome, so the directory is written into the configuration path file, the configuration file is divided into.bash_profile
and.zshrc
, is invalid every time the terminal is shut down, and needs to be executed when the terminal is startedsource ~/.bash_profile
The directive puts the configuration file into effect; The latter is a global configuration file, that is, the startup file is recommended.zshrc
Configured (as shown below)
export WORKON_HOME=~/.virtualenvs
Specify the virtual environment directory (in this case, enter the virtual environment directory created by yourself)export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
Specified usepython3
Environment Manages virtual environmentssource /usr/local/bin/virtualenvwrapper.sh
That is, when the configuration file is executedvirtualenvwrapper
To take effectsource ~/.bash_profile
Let the terminal configuration file also take effect.alias python="/usr/bin/python3"
andalias pip="/usr/bin/pip3"
Defines which directory to execute inpython
File to execute, preceding#
Comment is not valid.
References:
MAC OSX correctly installs Python at the same time as VirtualWrapper in python3 Python3: Python and Pycharm virtualenv