Added more tools for virtual env (#31415)
* Added more tools for virtual env Based on Python official documentation * typo in 'h1' Correct value: Environments * another typo Correct value: together
This commit is contained in:
committed by
Christopher McCormack
parent
40c17b6ba1
commit
828375d462
@ -2,7 +2,7 @@
|
||||
title: Virtual Environments
|
||||
---
|
||||
|
||||
## Virtual Environments
|
||||
# Virtual Environments
|
||||
|
||||
Virtual environments can be described as isolated installation directories. This isolation allows you to localize the installation of your project's dependencies, without forcing you to install them system-wide.
|
||||
|
||||
@ -14,7 +14,10 @@ Benefits:
|
||||
|
||||
Here are three ways you can create Python virtual environments.
|
||||
|
||||
## Virtualenv
|
||||
## Tools used for creating Python virtual environments
|
||||
|
||||
### Libraries
|
||||
#### 1. Virtualenv
|
||||
|
||||
[`virtualenv`](https://virtualenv.pypa.io/en/stable/) is a tool used to create isolated Python environments. It creates a folder which contains all the necessary executables to use the packages that a Python project would need.
|
||||
|
||||
@ -34,7 +37,6 @@ To create a virtual environment use:
|
||||
```
|
||||
virtualenv --no-site-packages my-env
|
||||
```
|
||||
|
||||
This creates a folder in the current directory with the name of the environment (`my-env/`). This folder contains the directories for installing modules and Python executables.
|
||||
|
||||
You can also specify the Python version you want to work with. Just use the argument `--python=/path/to/python/version`. For instance, `python2.7`:
|
||||
@ -42,14 +44,12 @@ You can also specify the Python version you want to work with. Just use the argu
|
||||
virtualenv --python=/usr/bin/python2.7 my-env
|
||||
```
|
||||
|
||||
### List Environments
|
||||
|
||||
You can list the available environments with:
|
||||
```
|
||||
lsvirtualenv
|
||||
ls virtualenv
|
||||
```
|
||||
|
||||
### Activate an Environment
|
||||
Activate an Environment:
|
||||
|
||||
Note : On Windows, activating a virtual environment requires the user to have the permission to run scripts.
|
||||
|
||||
@ -60,18 +60,16 @@ For Mac OS or Linux systems :
|
||||
```
|
||||
source my-env/bin/activate
|
||||
```
|
||||
And for Windows :
|
||||
|
||||
And for Windows:
|
||||
|
||||
```
|
||||
.\my-env\Scripts\activate.bat
|
||||
```
|
||||
|
||||
This ensures that only packages under `my-env/` are used.
|
||||
|
||||
You will notice that the name of the environment is shown on the left of the prompt. This way you can see which is the active environment.
|
||||
|
||||
### Install Packages
|
||||
|
||||
You can install packages one by one, or by setting a `requirements.txt` file for your project.
|
||||
```
|
||||
pip install some-package
|
||||
@ -82,26 +80,49 @@ If you want to create a `requirements.txt` file from the already installed packa
|
||||
```
|
||||
pip freeze > requirements.txt
|
||||
```
|
||||
|
||||
The file will contain the list of all the packages installed in the current environment, and their respective versions. This will help you release your project with its own dependent modules.
|
||||
|
||||
|
||||
### Deactivate an Environment
|
||||
|
||||
If you are done working with the virtual environment you can deactivate it with:
|
||||
```
|
||||
deactivate
|
||||
```
|
||||
|
||||
This puts you back to the system’s default Python interpreter with all its installed libraries.
|
||||
|
||||
### Delete an Environment
|
||||
To delete an Environment simply delete the environment folder.
|
||||
|
||||
Simply delete the environment folder.
|
||||
#### 2. venv
|
||||
|
||||
## Pipenv
|
||||
[`venv`](https://docs.python.org/3/library/venv.html) is available by default in Python 3.3 and later.
|
||||
|
||||
[`pipenv`](https://pipenv.readthedocs.io/en/latest/) is a tool that automatically creates and track packges in your virtual environments. It combines `pip` and `virtualenv` together so you do need to use them separately.
|
||||
To create a virtual environment use:
|
||||
```
|
||||
python3 -m venv venv
|
||||
```
|
||||
|
||||
|
||||
#### 3. pyenv
|
||||
|
||||
[`pyenv`](https://github.com/yyuu/pyenv) simple Python version management based on [`rbenv`](https://github.com/rbenv/rbenv).
|
||||
Used together with [`pyenv-virtualenv`](https://github.com/pyenv/pyenv-virtualenv) plugin
|
||||
Create an Environemnt:
|
||||
|
||||
To create a virtual environment use:
|
||||
```
|
||||
pyenv virtualenv venv34
|
||||
```
|
||||
|
||||
|
||||
#### 4. pyvenv
|
||||
|
||||
Deprecated in Python 3.6.
|
||||
|
||||
|
||||
### Dependency managers
|
||||
#### 1. Pipenv
|
||||
|
||||
[`Pipenv`](https://docs.pipenv.org/) manages dependencies on a per-project basis. It is like Node.js’ [`npm`](https://www.npmjs.com/) or Ruby’s [`bundler`](https://bundler.io/).
|
||||
|
||||
`pipenv` automatically creates and track packges in your virtual environments. It combines `pip` and `virtualenv` together so you do need to use them separately.
|
||||
|
||||
You can install it with `pip`:
|
||||
```
|
||||
@ -166,19 +187,18 @@ pipenv --rm
|
||||
|
||||
You can also delete the environment by deleting the project directory.
|
||||
|
||||
## Conda
|
||||
|
||||
## Separate software stacks
|
||||
#### 1. Conda
|
||||
|
||||
[`Conda`](https://conda.io/docs/index.html) is a package, dependency and environment management for many languages, including Python.
|
||||
|
||||
To install Conda, follow these [instructions](https://conda.io/docs/user-guide/install/index.html).
|
||||
|
||||
### Create an Environment
|
||||
|
||||
To create a virtual environment use:
|
||||
```
|
||||
conda create --name my-env
|
||||
```
|
||||
|
||||
Conda will create the corresponding folder inside the Conda installation directory.
|
||||
|
||||
You can also specify which version of Python you want to work with:
|
||||
@ -186,115 +206,32 @@ You can also specify which version of Python you want to work with:
|
||||
conda create --name my-env python=3.6
|
||||
```
|
||||
|
||||
### List Environments
|
||||
|
||||
You can list all the available environments with:
|
||||
```
|
||||
conda info --envs
|
||||
```
|
||||
|
||||
### Activate an Environment
|
||||
|
||||
Before you can start using the environment you need to activate it:
|
||||
```
|
||||
source activate my-env
|
||||
```
|
||||
|
||||
### Install Packages
|
||||
|
||||
The same as with `virtualenv`.
|
||||
|
||||
### Deactivate an Environment
|
||||
Install packages like with `virtualenv`.
|
||||
|
||||
If you are done working with the virtual environment you can deactivate it with:
|
||||
```
|
||||
source deactivate
|
||||
```
|
||||
|
||||
### Remove an Environment
|
||||
|
||||
If you want to remove an environment from Conda use:
|
||||
```
|
||||
conda remove --name my-env
|
||||
```
|
||||
|
||||
## Pyenv
|
||||
|
||||
[`Pyenv`](https://github.com/pyenv/pyenv) is a package that lets you manage multiple versions of Python.
|
||||
|
||||
To use Pyenv to manage our virtual environments we are going to install two additional extensions below:
|
||||
|
||||
```
|
||||
brew install pyenv
|
||||
brew install pyenv-virtualenv
|
||||
brew install pyenv-virtualenvwrapper
|
||||
```
|
||||
|
||||
### Update .bashrc
|
||||
Add this command to your .bashrc file to make sure your virtual environment is initialized everytime you start a new terminal session.
|
||||
|
||||
For example using Vim I open up terminal and type:
|
||||
```
|
||||
vim ~/.bashrc
|
||||
```
|
||||
Then I add the below command to the file:
|
||||
|
||||
```
|
||||
eval "$(pyenv init -)"
|
||||
```
|
||||
|
||||
### Install Python versions
|
||||
It's usually good to have at least both a Python 2 and Python 3 environment
|
||||
|
||||
```
|
||||
pyenv install 3.6.0
|
||||
pyenv install 2.7.13
|
||||
```
|
||||
|
||||
### Create an Environment
|
||||
|
||||
To create a virtual environment with Python 3 as the base with the name "my_cool_app":
|
||||
```
|
||||
pyenv virtualenv 3.6.0 my_cool_app
|
||||
```
|
||||
It's that easy. Another example using the Python 2.7.13 base that we created earlier:
|
||||
|
||||
```
|
||||
pyenv virtualenv 2.7.13 my_cool_app
|
||||
```
|
||||
|
||||
### Activate an Environment
|
||||
|
||||
Before you can start using the environment you need to activate it:
|
||||
```
|
||||
pyenv activate my_cool_app
|
||||
```
|
||||
|
||||
### Install Packages
|
||||
|
||||
First activate your virtual environment and then use pip like normal
|
||||
|
||||
```
|
||||
pyenv activate my_cool_app
|
||||
pip install requests
|
||||
```
|
||||
|
||||
### Deactivate an Environment
|
||||
|
||||
If you are done working with the virtual environment you can deactivate it with:
|
||||
```
|
||||
pyenv deactivate
|
||||
```
|
||||
|
||||
### Remove an Environment
|
||||
|
||||
If you want to remove the Python 2.7.13:
|
||||
```
|
||||
pyenv uninstall 2.7.13
|
||||
```
|
||||
|
||||
#### More Information:
|
||||
* `virtualenv` [official website](https://virtualenv.pypa.io/en/stable/)
|
||||
* `pipenv` [official website](https://pipenv.readthedocs.io/en/latest/)
|
||||
* `Conda` [official website](https://conda.io/docs/index.html)
|
||||
* `The Hitchhicker's Guide to Python` - [Pypenv & Virtual Environments](http://docs.python-guide.org/en/latest/dev/virtualenvs/)
|
||||
## Additional Resources:
|
||||
- [Python Packaging Authority - Tool recommendations](https://packaging.python.org/guides/tool-recommendations/)
|
||||
- [Python Packaging Authority - Creating Virtual Environments](https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments)
|
||||
- `virtualenv` [official website](https://virtualenv.pypa.io/en/stable/)
|
||||
- `pipenv` [official website](https://pipenv.readthedocs.io/en/latest/)
|
||||
- `Conda` [official website](https://conda.io/docs/index.html)
|
||||
- `The Hitchhicker's Guide to Python` - [Pypenv & Virtual Environments](http://docs.python-guide.org/en/latest/dev/virtualenvs/)
|
||||
|
Reference in New Issue
Block a user