How to Setup Virtual Environment Using Pipenv

Virtual Environments using PipEnv

PipEnv is a new way to create Virtual Environments in Python that allows for Environment Agnostic dependency installation.

The following information will help you install PipEnv to get started.

Installing PipEnv

Start with installing Python 3.x (I used Python 3.7.1 as of this writing.)

Once you have python3 installed. Use pip to install PipEnv.

At the terminal write:

1
pip install --user pipenv

Because I have both python 2.7 and python 3.7 installed, I had to use pip3 to install pipenv for python3.

You might have to update the PATH environment variable in order for the command to run from the terminal. For more information about setting your OS’s Environment Variable click here.

For more information on how to install PipEnv go to the documentation site at: pipenv.readthedocs.io/en/latest/

Next, we are going to set up the requirements.txt file to make sure you have all the necessary dependencies for the rest of the book.

Versions – Requirements.txt File

The versions that you will need to follow this tutorial is provided for you by the following requirements.txt file.

The instructions that you need to follow are also provided and should work on any Operating System. Here is the requirements.txt file that we are going to use:

1
2
Django==2.2
djangorestframework==3.9.2

Copy and Paste the above in a file labeled requirements.txt in the root directory of your app. Make sure you are inside of that directory. Then, run the following command:

1
pipenv install -r requirements.txt

Using Pip inside your virtual environment

An alternative to the requirements.txt file above is to use pip inside your Virtual Environment.

1
2
3
pipenv shell
pip install django
pip install djangorestframework

Once it’s finished, you should have a PipFile file and a PipFile.lock file.

Congratulations! You installed PipEnv and you have an environment that is ready to go for the development for the rest of the book.

Everytime you sit at the computer you should do this…

When you’re ready to sit down and work on your project, you should do the following:

Open up your terminal and go to the directory with the PipFile file and a PipFile.lock file. Then, type:

1
pipenv shell

Your shell should be changed – it should be noticeably different. Mine looks like this:

1
(token_auth_app) bash-3.2$

Yours should be similar, although what is inside of the parentheses might be different.

Everytime you are done working on your app you should do this…

When you’re done working, you should deactivate the Virtual Envionment by running the following two commands:

1
2
deactivate
exit

This will kick you out of the Virtual Environment so you don’t mess up the virtual environment for later.

comments powered by Disqus