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

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.
主站蜘蛛池模板: 舒城县| 湖南省| 托里县| 唐海县| 深圳市| 金沙县| 抚宁县| 岫岩| 崇信县| 宣汉县| 保山市| 平度市| 珲春市| 介休市| 鲜城| 观塘区| 安新县| 泾阳县| 河源市| 长子县| 双柏县| 鹰潭市| 嘉义市| 台南市| 霍邱县| 尼木县| 固原市| 安陆市| 紫金县| 正宁县| 安义县| 察隅县| 应城市| 丰原市| 顺昌县| 铁力市| 新龙县| 翁牛特旗| 四会市| 墨竹工卡县| 合水县|