The following steps come from The Hitchhiker's Guide.
Assume all instructions are for python 3.x version.
NOTE pipenv and virtualenv are indepenent of each other. I personally prefer virtualenv because of how smooth it can be with virtualenvwrapper.
-
Ensure python is installed by running this at command prompt or Anaconda:
python --version
-
Ensure pip is available:
pip --version
-
Install dependency manager for your projects:
pip install --user pipenv
-
When ready to install packages for your project:
- Change into your project directory:
cd DIRECTORY
- Run:
pipenv install MODULENAME
(where MODULENAME is something like requests)
This installs the library & creates a Pipfile (used to track dependencies in case you need to reinstall them).
- Change into your project directory:
-
When looking to run your script:
pipenv run python FILENAME.py
Using
pipenv run
ensures your installed packages are available to your script.
More on this was covered in Virtual Environments.
Basically virtualenv is a tool to create isolated Python environments. It creates a folder that has all the necessary executables to use the packages your project would need.
It can also be used standalone - in place of Pipenv!
-
Install via pip:
pip install virtualenv
-
Test your installation:
virtualenv --version
-
Change into the directory of your project:
cd DIRECTORY
-
Create a virtual environment:
virtualenv ENVNAME
If you omit the name of the environment, this will not create a folder for all the executable files & copy of your pip library. It will just put them in the current folder.
NOTE: conventional name is to use venv as it is readily available in ignore files.
For fancier commands, check out the original MD file.
- Activate your virtual environment
- Linux:
source VENVNAME/bin/activate
- PC:
\venv\Scripts\activate
- Linux:
The name of the current virtual environment will now appear on the left of the prompt to let you know that it’s active. From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.
-
Install packages as normal
-
When you have your packages, it is best practice to "freeze" the current state of environment packages to a file:
pip freeze > requirements.txt
You can see the list by running:
pip list
You are able to install the requirements later by running:
pip install -r requirements.txt
-
Be sure to add your venv to the gitignore list.
-
When done with venv:
deactivate
-
To delete you just need to delete the folder of the venv:
rm -rf NAME
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.
-
Ensure that python, pip, and virtualenv are installed.
-
Install the wrapper (full instructions here)
- linux:
$ pip install virtualenvwrapper $ export WORKON_HOME=~/Envs $ source /usr/local/bin/virtualenvwrapper.sh
- PC (default PATH for WORKON_HOME is %USERPROFILE%\Envs):
pip install virtualenvwrapper-win
- linux:
I will provide two options - and highly suggest the 2nd.
-
Create a venv:
mkvirtualenv VENVNAME
This creates the VENVNAME folder inside
~/Envs
. -
Work on the virtual environment:
workon VENVNAME
Maybe you have multiple python versions on your machine - what then?
It's very simple. You will use this command instead: mkvirtualenv -p "\Folder Of\PythonVER\python.exe" VENVNAME
Unless you changed the name of the python executable, that's basically the gist. And you can still create a project!
You can make a project, which creates the virtual environment, and also a project directory inside $WORKON_HOME
, which is cd
-ed into when you workon PROJECTNAME
.
-
Create project:
mkproject PROJECTNAME
The first time you run this, you will see a similar message:
(base) C:\Users\Kat\Documents\SOME_DIRECTORY>mkvirtualenv xlsxwriter-autofit C:\Users\Kat\Envs is not a directory, creating Using base prefix 'c:\\programdata\\anaconda3' New python executable in C:\Users\Kat\Envs\xlsxwriter-autofit\Scripts\python.exe Installing setuptools, pip, wheel... done. (xlsxwriter-autofit0) (base) C:\Users\Kat\Documents\Programming\repo\Personal-Projects\xlsxwriter-autofit>
This is because prior to this, you did not yet have the Envs folder that is used to store these virtual environments.
Since you do not have to be in the root of your project folder to do this, you can run while in any directory. This will (by default) also activate the environment.
-
Set your project directory by creating or navigating to the root folder of your project and then run:
setprojectdir
When you set your project directory, the next time you activate your virtual environment it will:
- activate the virtual environment
- automatically move into this directory
-
To deactivate:
deactivate
You can activate your virtual project to work on things again by running: workon PROJECTNAME
To delete a virtual environment, run: rmvirtualenv venv
Not sure how to delete a virtual project, since the following doesn't work: rmproject venv
Additional items can be found here.
NOTE: If you delete a venv, you can recreate it and then tie it & it's project together in another folder.
There may come a time where you wish to duplicate a virtual environment.
Thanks to this medium post here's how.
cpvirtualenv ENVNAME TARGETENVNAME
By reading the docs.
Likely you need to set up your environment variables. Checkout the SetEnvVars doc to see the steps I took to resolve on my Windows machine.
Basically this StackOverflow response was probably the only thing that needed to be done.