Installation and use of python virtual environment virualenv

  • 2020-05-19 05:05:19
  • OfStack

preface

After installing python, pip, setuptools and other tools, virualenv virtual environment can be created. This tool is similar to a virtual machine, which can run multiple versions of python programs on the same computer without affecting each other. When not in use, you can exit or delete it.

1. Install virtualenv


#install pip on mac
brew install python
curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python
sudo easy_install pip
#install virtualenv by pip
pip install virtualenv

2. Use of virtualenv


# create 1 called pythonEnv The new environment 
virtualenv pythonEnv
# Activated reuse 
cd pythonEnv
source bin/activate
# Out of the environment 
deactivate

3. Use virtualenvwrapper to manage the virtual environment

Install virtualenvwrapper


pip install virtualenvwrapper

Configure environment variables:


vim ~/.bash_profile
# Virtualenv/VirtualenvWrapper
source /usr/local/bin/virtualenvwrapper.sh
# exit vim
source ~/.bash_profile

Create an environment


mkvirtualenv pythonEnv # in  ~/Envs  Created in the  pythonEnv folder 
mkvirtualenv python3Env -p python3.5 # create python3.5 The environment of 

Switching environment:


workon pythonEnv

Exit environment:


deactivate

Delete environment:


rmvirtualenv pythonEnv

other

1. Other commands


lsvirtualenv # List all the circumstances. 
cdvirtualenv # Navigate to the directory of the currently active virtual environment so that you can browse through it, for example  site-packages  . 
cdsitepackages # Similar to the one above, but straight in  site-packages  Directory. 
lssitepackages # According to  site-packages  The contents of the directory. 

Resources: http: / / virtualenvwrapper readthedocs. io en/latest/command_ref html

2. ImportError: No module named extern error occurs when pip is installed using easy_install command

Reason: mac's built-in python2.7.12 extern module is not installed

Solutions:


#download from https://pypi.python.org/pypi/extern/0.1.0
tar zxf extern-0.1.0.tar.gz && python setup.py install

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: