Pipenv allows the creation of a virtual environment for a Python project and manages dependencies through the Pipfile.
Pipenv ensures that each project uses the correct version of each package, and that each of those packages has the correct dependencies.
Installing Pipenv
Make sure you have python and pip installed.
// once you have python installed, use pip to install pipenv:
$ pip install --user pipenv
In case you got a warning that some scripts is not on PATH, you should follow the next steps:
PATH is an environment variable specifying a set of directories where executable programs are located.
Adding Pipenv to Windows 10 PATH Environment Variable
- Open up the Control Panel and navigate to System and Security > System
- Click on the Advanced system settings
- Click Environment Variables
- Under User Variables, double-click the variable PATH
- Click New, and add the directory where scripts was installed, e.g. “C:\Users\user\AppData\Roaming\Python\Python39\Scripts”, and select OK
You will probably need to restart PowerShell or Command Prompt window to pick up the change.
// to test it, in new PowerShell window, type:
$env:PATH
Setting up Python virtual environment using pipenv
To set up a new virtual environment for a project, create an empty directory.
Open a console in this directory and type pipenv install <package_name> to install a package for the project.
To specify that the package is for development, use the -d flag.
$ pipenv install is used for installing packages into the pipenv virtual environment and updating your Pipfile.
You can run $ pipenv install -r requirements.txt to import a requirements file.
// If virtual environment doesn’t exist, it’ll create it
$ pipenv install django
Once it’s finished, you should have a PipFile file and a PipFile.lock file in the project directory. Pipfiles contain information for the dependencies of the project.
The virtual environment is not created in the project directory itself. It’s created in a directory managed by Pipenv in a user profile.
To activate the environment, just navigate to your project directory and use pipenv shell to launch a new shell session or use pipenv run <command> to run a command directly.
When you’re done working, deactivate the virtual environment by running the following command:
$ exit
To create a new django project at the terminal write:
$ pipenv shell
$ django-admin startproject django_project .
Run a built-in web server:
$ python manage.py runserver
Open a browser at http://127.0.0.1:8000 and see Django welcome page.
You should be able to see the following files and folders of created Django project:
│ db.sqlite3
│ main.py
│ manage.py
│ Pipfile
│ Pipfile.lock
│
└───django_project
asgi.py
settings.py
urls.py
wsgi.py
__init__.py
Additional examples of pipenv usage
To upgrade pipenv at any time:
$ pip install --user --upgrade pipenv
To see dependency graph:
$ pipenv graph
To completely remove all the installed packages from your virtual environment:
$ pipenv uninstall --all
To see where the virtual environment is:
// locates for a particular project
$ pipenv –venv
To see where the project home is:
$ pipenv --where
To delete the current virtual environment:
// This will not remove the Pipfiles
$ pipenv –rm
To update specific package:
$ pipenv update package_name
To updates all packages:
$ pipenv update