Use virtualenv and virtualenvwrapper in Mac

Virtualenv #

Introduction #

In the process of developing with Python, it is inevitable that you will encounter problems with different projects relying on different versions of libraries, or that you do not want to flood your physical environment with various libraries during development and cause future dependency disasters.

Therefore, we need to use different virtual environments for different projects to keep the development environment and the host environment clean. virtualenv is a great tool to help us manage our different Python environments. virtualenv allows you to create several different virtual environments on your system that do not interfere with each other.

Install #

 pip3 install virtualenv

This works

Use #

to create #

If we want to use scrapy to crawl a website and we don't want to install scrapy and requests in our host environment, we can use virtualenv.

Suppose we put this virtual environment in the ~/workspaces/project_env/spider/ directory

 virtualenv ~/workspaces/project_env/spider/

This creates the virtual environment, and we can see that three directories are created under this directory

  • bin: contains the commands available in the virtual environment, and the script to open the virtual environment activate.
  • include: contains the headers of the virtual environment, including the Python headers
  • lib: This is the library dependencies

activates #

 source ~/workspaces/project_env/spider/bin/activate

At this point we are already in the virtual environment

You can install the requests module

 pip install requests

As you can see, it works very quickly

exit the virtual environment #

 deactivate

virtualenvwrapper #

Introduction #

We just learned about virtualenv, and I think it's a bit of a hassle to source the bin directory under which the virtual environment is located before opening it each time activate, which requires us to remember the directory where each virtual environment is located.

A possible solution is to centralize all the virtual environment directories, for example in ~/virtualenvs/, and use different directories for different virtual environments. virtualenvwrapper does exactly that. It also eliminates the need for source operations each time a virtual environment is opened, making it much more usable.

Install #

 pip install virtualwrapper

This way we have installed the magic tool that can manage virtual environments

Use #

configuration #

First you need to configure virtualenvwrapper:

  • You need to specify an environment variable called WORKON_HOME, which is the directory where the various virtual environment directories are stored
  • You need to export vitualenvwrapper where the module is stored.
  • need to run its initialization tool virtualenvwrapper.sh, you can check the location with which virtualenvwrapper.sh, mine is in /usr/local/bin/

Since we need to perform these two steps every time, we can write them to the terminal's configuration file.

If using bash, add it to ~/.bashrc

If you use zsh, add it to ~/.zshrc

This will automatically run every time you start the terminal, and virtualenvwrapper will be available after the terminal starts

 export WORKON_HOME='~/Workspaces/Envs'

 export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3

 source /usr/local/bin/virtualenvwrapper.sh

Create a virtual machine

mkvirtualenv env

After creating the virtual environment, you will automatically switch to the created virtual environment

Of course, you can also specify the python version of the virtual machine

mkvirtualenv env -p C:\python27\python.exe

List of virtual environments

workon or lsvirtualenv

Start/switch virtual environments

Use workon [virtual-name] to switch to the corresponding virtual environment

workon [virtual-environment-name]

delete virtual environment

rmvirtualenv [virtual environment name]

leave virtual environment, same command as virutalenv

deactivate

Tags:

Python