pip install virtualenv
$ cd my_project_folder
$ virtualenv venv
After executing the command, it creates a folder in the current directory, which contains some Python implementation files, as well as a copy of the pip for installing the other packages.
In addition, when you create env can choose the specified Python interpreter, for example, the following is based on Python3 created a virtual environment.
$ virtualenv -p /usr/local/bin/python3 venv
By default, the virtual environment will depend on the system components in the system environment, that is, the system has been installed in a third party package will be installed in a virtual environment, if you do not want to rely on these packages, then you can add the parameters-no-site-packages to create a virtual environment
virtualenv –no-site-packages [Virtual environment name]
Activate the virtual environment
After the activation is successful, the command line will display the name of the virtual environment, similar to ((env)Your-Computer:your_project UserName$)
Exit the virtual environment
If you want to delete the virtual environment, then you can directly run the rm -rf venv/ command.
Install Python packages in a virtual environment
Virtualenv comes with a pip installation tool, so the packages that need to be installed can run directly:pip install [Package name]
If you do not start the virtual environment, the system environment also installed the pip tool, then the packages will be installed in the system environment, in order to avoid this, you can add in the ~ / .bashrc file:export PIP_REQUIRE_VIRTUALENV=true
If you run pip without opening a virtual environment, you will be prompted with an error:
Could not find an activated virtualenv (required).
Virtaulenvwrapper is an extension package for virtualenv for more convenient management of virtual environments. It can be done:
1. Integrate all virtual environments in a single directory
2. Manage (add, delete, copy) virtual environments
3. Switch virtual environments
Before installing Virtualenvwrapper, virtualenv is required to be installed$ pip install virtualenvwrapper
At this time can not use virtualenvwrapper, the default virtualenvwrapper installed in /usr/local/bin below, in fact, you need to run virtualenvwrapper.sh file.
Create a directory to store the virtual environment
export WORKON_HOME=$HOME/Envs source /usr/local/bin/virtualenvwrapper.sh 3. Run: $ source ~/.zshrc
At this point, virtualenvwrapper can be used. The basic use of virtualenvwrapper:
1. List the virtual environment listworkon or lsvirtualenvCreate new virtual environment mkvirtualenv [virtual environment name]
2. Start/switch virtual environment
workon [virtual environment name]3. Remove the virtual environment
rmvirtualenv [virtual environment name]
4. Leave the virtual environmentdeactivate
The post Introduction to Python Virtualenv appeared first on Penetration Testing.