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

  1. 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)
  2. The installationpython3Environment:brew install python3
  3. Install virtual environment:sudo pip3 install virtualenv
  4. Install the add, delete, change, and check virtual environment extension package:sudo pip3 install virtualenvwrapper
  5. Create a virtual environment directory:mkdir ~/.virtualenvs
  6. Find the path to python3 and VirtualenvWrapper. sh:which python3andwhich virtualenvwrapper.sh
  7. Open the global configuration file:open ~/.zshrc
  8. 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
  1. Compile to put the configuration into effect:source ~/.zshrc
  2. Create a virtual environment:mkvirtualenv -p python3 xxx
  3. Open thePycharmYou can choose a virtual environment for development!

Two. Personal understanding after stepping pit matters needing attention

  1. The original Mac comes with Python2. They usually useHomeBrewforpython2andpython3The purpose of the reinstallation is to recreate the environment used for development, isolated from the system environment. At presentHomeBrewNo longer searchablepython2Python2 is no longer maintained and will be used in the futurepython3Development, so the impact is not big, I use direct abandonpython2Management of the environment.
  2. Used when installing virtualenv and VirtualenvWrappersudo pip3 installInstructions to install as the system defaultspython2.
  3. mkdir ~/.virtualenvsDirectives 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
  4. usewhichCommand to query the file directory and record it (which python3andwhich virtualenvwrapper.sh)
  5. After the installation is complete, you need to configure the path because you need to specify the virtual environment directory and virtual environment directorypythonVersion, 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_profileThe directive puts the configuration file into effect; The latter is a global configuration file, that is, the startup file is recommended.zshrcConfigured (as shown below)

  • export WORKON_HOME=~/.virtualenvsSpecify the virtual environment directory (in this case, enter the virtual environment directory created by yourself)
  • export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3Specified usepython3Environment Manages virtual environments
  • source /usr/local/bin/virtualenvwrapper.shThat is, when the configuration file is executedvirtualenvwrapperTo take effect
  • source ~/.bash_profileLet 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