官术网_书友最值得收藏!

Setting up a virtual environment

Now you can learn to set up a virtual environment that will help to set up an isolated scripting environment. This will help us to keep the dependencies required by different projects in different locations. Also, it helps to keep the global site-packages clean and separate from project dependencies:

  1. You can use pip to install the virtual environment module in the system:
$ pip install virtualenv 
  1. Then test the installation by using the following:
$ virtualenv --version 
  1. Try creating a new virtual environment inside your project folder:
$ mkdir new-project-folder
$ cd new-project-folder
$ virtualenv new-project

This will create a folder in the current directory with a name new-project.

If you want to create a virtual environment with a Python interpreter of your choice as follows:

$ virtualenv -p /usr/bin/python3 new-project  
  1. You can activate this virtual environment with the following:
$ source new-project/bin/activate  
  1. If you have completed your work inside the virtual environment, you can deactivate and get out of the virtual environment with the following: 
$ deactivate  
  1. We can make it simpler with virtualenvwrapper. The virtualenvwrapper helps to keep all our virtual environments in one place. To install virtualenvwrapper we can use the pip command:
$ pip install virtualenvwrapper  

We have to set the WORKON_HOME variable, which is the folder where all the virtual environments are saved:

$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh  
  1. With virtualenvwrapper we can create a project as follows:
$ mkvirtualenv new-project  

This will create the virtual environment inside the WORKON_HOME, that is, ~/Envs.

  1. To activate the created project we can use the following command:
$ workon new-project    
  1. With more ease we can create a virtual environment and the project folder with a single command as follows:
$ mkproject new-project    
  1. Finally, we can exit from the virtual environment with the deactivate command itself.
主站蜘蛛池模板: 兴安县| 蚌埠市| 武乡县| 南安市| 东乡族自治县| 贞丰县| 抚远县| 逊克县| 霍城县| 始兴县| 建始县| 泰州市| 开鲁县| 巩留县| 安乡县| 舒城县| 静海县| 淳化县| 湘潭县| 盐边县| 吉安县| 定日县| 泰顺县| 宕昌县| 九龙坡区| 响水县| 拉孜县| 怀柔区| 仁布县| 繁峙县| 红安县| 杭锦后旗| 蒲江县| 民乐县| 五家渠市| 南宫市| 阿拉善右旗| 贵德县| 鸡西市| 湖北省| 兴宁市|