Install pipenv Windows

Summary: in this tutorial, you’ll learn how to install the pipenv packaging tool on Windows and how to configure a project with a new virtual environment using the Python pipenv tool.

Prerequisites

Before installing the pipenv tool, you need to have Python and pip installed on your computer.

First, open the Command Prompt or Windows Powershell and type the following command.

python -V

Note that the letter V in the -V is uppercase. If you see the Python version like the following:

Python 3.8.5Code language: CSS (css)

…then you already have Python installed on your computer. Otherwise, you need to install Python first.

Second, use the following command to check if you have the pip tool on your computer:

pip -V

It’ll return something like this:

pip 20.2.4 from C:\Users\<username>\AppData\Roaming\Python\Python38\site-packages\pip (python 3.8)    Code language: CSS (css)

Install pipenv on Windows

First, use the following command to install pipenv tool:

pip install pipenv

Second, replace your <username> in the following paths and add them to the PATH environment variable:

c:\Users\<username>\AppData\Roaming\Python\Python38\Site-Packages
C:\Users\<username>\AppData\Roaming\Python\Python38\ScriptsCode language: HTML, XML (xml)

It’s important to notice that after changing the PATH environment variable, you need to close the Command Prompt and reopen it.

Third, type the following command to check if the pipenv installed correctly:

pipenv -h

If it shows the following output, then you’ve successfully installed the pipenv tool successfully.

Usage: pipenv [OPTIONS] COMMAND [ARGS]...
...Code language: CSS (css)

However, if you see the following message:

pipenv shell 'pipenv' is not recognized as an internal or external command, operable program or batch file.Code language: JavaScript (javascript)

Then you should check step 2 to see if you have already added the paths to the PATH environment variable.

In this tutorial, you have learned how to install the pipenv tool on Windows computers.

Creating a new project

First, create a new project folder e.g., crawler.

Second, navigate to the crawler folder and install the requests package using the pipenv command:

pipenv install requests

Output:

Creating a Pipfile for this project…
Installing requests…
Adding requests to Pipfile's [packages]…
Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Locking...Building requirements...
Resolving dependencies...
Success!
Updated Pipfile.lock (fbd99e)!
Installing dependencies from Pipfile.lock (fbd99e)…
================================ 0/0 - 00:00:00

And you’ll see that pipenv created two new files called Pipfile and Pipfile.lock. On top of that, it installed a virtual environment.

If you look at the project folder, you won’t see the virtual environment folder.

To find the location of the virtual environment, you use the following command:

pipenv --venv

It’ll return something like this on Windows:

C:\Users\<username>\.virtualenvs\crawler-7nwusESRCode language: HTML, XML (xml)

Note that the <username> is the username that you use to log in to Windows.

Third, create a new file called app.py in the project folder and add the following code to the file:

import requests

response = requests.get('https://www.python.org/')
print(response.status_code)Code language: JavaScript (javascript)

In this code, we imported the requests third-party module, use the get() function to make an HTTP request to the URL https://www.python.org/ and display the status code (200).

Fourth, run the app.py file from the terminal by using the python command:

python app.pyCode language: CSS (css)

It’ll show the following error:

ModuleNotFoundError: No module named 'requests'Code language: JavaScript (javascript)

The reason is that Python couldn’t locate the new virtual environment. To fix this, you need to activate the virtual environment.

Fifth, use the following command to activate the new virtual environment:

pipenv shell

If you run the app.py now, it should work correctly:

python app.pyCode language: CSS (css)

Output:

200

The status code 200 means the HTTP request has succeeded.

Sixth, use the exit command to deactivate the virtual environment:

exitCode language: PHP (php)

Resolving the Unresolved Import Warning in VS Code

If you’re using VS Code, you may receive an unresolved import warning. The reason is that the VS code doesn’t know which Python interpreter to use.

Therefore, you need to switch the Python interpreter to the one located in the new virtual environment:

First, click the current Python interpreter at the right bottom corner of the VS Code:

Second, select the Python interpreter from the list:

Python pipenv - select Python Interpreter

In addition, you need to change the python.jediEnabled parameter in the settings.json to True:

To open the settings.json file, you open the Command Palette with the keyboard shortcut CTRL + SHIFT + P on Windows or CMD + SHIFT + P on macOS:

Python pipenv - open settings JSON format

And the change the value to True as follows:

Python pipenv - python.jediEnabled True

After that, you should save the file and restart the VS Code for the change.

Did you find this tutorial helpful ?