- Install python from https://www.python.org/downloads
- Get pip from https://bootstrap.pypa.io/get-pip.py
- Run python get-pip.py
Pipenv is a dependency manager for Python projects. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases.
$ pip install --user pipenv
$ cd myproject
$ pipenv install requests
$ pipenv run python main.py
$ pip install virtualenv
Test your installation
$ virtualenv --version
$ cd my_project_folder
$ virtualenv my_project
virtualenv my_project will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was my_project) can be anything; omitting the name will place the files in the current directory instead.
This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named my_project.
You can also use the Python interpreter of your choice (like python2.7).
$ virtualenv -p /usr/bin/python2.7 my_project
or change the interpreter globally with an env variable in ~/.bashrc:
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
$ source my_project/bin/activate
$ deactivate
virtualenvwrapper provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place.
To install (make sure virtualenv is already installed):
$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
Full instauctions: https://virtualenvwrapper.readthedocs.io/en/latest/install.html
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv my_project
mkvirtualenv my_project --python=python3
mkvirtualenv my_project --python=python2
$ pip install virtualenvwrapper-win
In Windows, the default path for WORKON_HOME is %USERPROFILE%Envs
- Create a virtual environment
Windows:
$ mkvirtualenv my_project
Linux:
$ mkproject my_project
This creates the my_project
folder inside ~/Envs
.
- Work on a virtual environment:
$ workon my_project
virtualenvwrapper provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names.
workon also deactivates whatever environment you are currently in, so you can quickly switch between environments.
- Deactivating is still the same:
$ deactivate
- To delete:
$ rmvirtualenv venv
python3 -m venv envname
only windows:
cd name
setproject dir .
lsvirtualenv
workon
List all of the environments.
cdvirtualenv
Navigate into the directory of the currently activated virtual environment, so you can browse its site-packages, for example.
cdsitepackages
Like the above, but directly into site-packages directory.
lssitepackages
Shows contents of site-packages directory.
When you cd into a directory containing a .env, autoenv automagically activates the environment.
Install it on Mac OS X using brew:
$ brew install autoenv
And on Linux:
$ git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
$ echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc
pip freeze > requirements.txt: save dependencies
pip install -r requirements.txt: install requirements
Links:
http://virtualenvwrapper.readthedocs.io/en/latest
http://docs.python-guide.org/en/latest/dev/virtualenvs
http://virtualenvwrapper.readthedocs.io/en/latest/install.html#basic-installation
http://sourabhbajaj.com/mac-setup/Python/virtualenv.html