virtualenv -- python virtual sandbox

Keywords: Ruby Python pip

notes preceding the text of a book or following the title of an article

It is said that virtualenv, fabric and pip are the three magic tools of Python.

Whether you agree or not, at least first, pip is often used now. virtualenv heard it for the first time, but I have to try it.

1, Installation

pip install virtualenv

Because I have already installed pip, it is easy and convenient to install directly with PIP.

For other installation methods, please refer to the official website: http://www.virtualenv.org/en/latest/index.html

2, Create a virtual environment

root@kali:/recall/code# virtualenv test_env
New python executable in test_env/bin/python
Installing setuptools, pip...done.
root@kali:/recall/code# 

Simply, it's the virtualenv environment name

By default, the virtual environment will depend on the site packages in the system environment, that is to say, the third-party packages already installed in the system will also be installed in the virtual environment,

If you don't want to rely on these package s, you can add parameters

--no-site-packages

Build a virtual environment

It becomes:

root@kali:/recall/code# virtualenv test_env --no-site-packages
New python executable in test_env/bin/python
Installing setuptools, pip...done.
root@kali:/recall/code# 

3, Start virtual environment

After the creation is successful, the corresponding directory file will be generated in the current directory.

root@kali:/recall/code# ls -l test_env/
//Total consumption 16
drwxr-xr-x 2 root root 4096  4 29 / 20:03 bin
drwxr-xr-x 2 root root 4096  4 29 / 19:58 include
drwxr-xr-x 3 root root 4096  4 29 / 19:58 lib
drwxr-xr-x 2 root root 4096  4 29 / 19:58 local
root@kali:/recall/code# 

Let's go to the directory first:

cd test_env/

Then start

root@kali:/recall/code/test_env# source ./bin/activate

After the startup is successful, the words "test" env will be added in front, as shown below

(test_env)root@kali:/recall/code/test_env# 

4, Use test

(test_env)root@kali:/recall/code/test_env# pip install requests
Downloading/unpacking requests
  Downloading requests-2.2.1-py2.py3-none-any.whl (625kB): 625kB downloaded
Installing collected packages: requests
Successfully installed requests
Cleaning up...
(test_env)root@kali:/recall/code/test_env# python
Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> 
>>> response = requests.get("http://www.baidu.com")
>>> response.status_code
200
>>> 

5, Exit virtual environment

deactivate

Complete as follows:

(test_env)root@kali:/recall/code/test_env# python
Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> 
>>> response = requests.get("http://www.baidu.com")
>>> response.status_code
200
>>> exit()
(test_env)root@kali:/recall/code/test_env# deactivate
root@kali:/recall/code/test_env# 

For more introductory tutorials, please refer to( http://www.bugingcode.com/python_start/)

Posted by cbrooks on Thu, 30 Apr 2020 15:51:32 -0700