Configure a pipenv environment
Pipenv is a tool that provides all necessary means to create a virtual environment for your Python project. It automatically manages project packages through the Pipfile file as you install or uninstall packages.
Pipenv also generates the Pipfile.lock file, which is used to produce deterministic builds and create a snapshot of your working environment. This might be particularly helpful for security sensitive deployment, when project requirements and packages versions are critical. For more information about pipenv, refer to the project documentation at pipenv.pypa.io.
To use pipenv with PyCharm, you need to implement several preparation steps.
Run the following command to ensure you have pip installed in your system:
$ pip --version
You should expect to receive a system response indicating the pip version. If no pip is discovered, install it as described in the Installation Instructions. Alternatively, you can download and install Python from http://python.org.
Install
pipenv
by running the following command:$ pip install --user pipenv
When installation completes, you will see the following message:
For your convenience, you might add the user base’s binary directory to your
PATH
environmental variable. If you skip this procedure, PyCharm will prompt you to specify the path to the pipenv executable when adding a pipenv environment.WindowsLinux and macOSRun the following command:
$ py -m site --user-site
A sample output can be:
C:
\Users \jetbrains \AppData \Roaming \Python \Python37 \site-packages Replace
site-packages
withScripts
in this path to receive a string for adding to thePATH
variable, for example:$ setx PATH "%PATH%;C:\Users\jetbrains\AppData\Roaming\Python\Python37\Scripts"
warning
Running the above command in PowerShell will overwrite the user PATH variable. To avoid that, use
$ setx PATH "$env:PATH;C:\Users\jetbrains\AppData\Roaming\Python\Python37\Scripts"
instead.However, you should keep in mind that this command has a side effect of duplicating the content of the system PATH variable in the user PATH variable.
For more information about managing environment variables in PowerShell, refer to the PowerShell documentation.
Run the following command to find the user base's binary directory:
$ python -m site --user-base
An example of output can be
/Users (macOS) or/jetbrains /.local /home (Linux)/jetbrains /.local Add bin to this path to receive a string for adding to the ~/.bashrc file, for example:
$ export PATH="$PATH:/Users/jetbrains/.local/bin"
Run the following command to make the changes effective:
$ source ~/.bashrc
Ensure you have enabled bashrc in your bash_profile.
At any time you can alter the specified path to the pipenv executable in the project settings. In the Settings dialog (CtrlAlt0S) , navigate to Tools | Python Integrated Tools, and type the target path in the Path to Pipenv executable field.
After the preparation steps are done, you can use pipenv to create a virtual environment for new or existing projects.
Do one of the following:
Click the Python Interpreter selector and choose Add New Interpreter.
Press CtrlAlt0S to open Settings and go to Project: <project name> | Python Interpreter. Click the Add Interpreter link next to the list of the available interpreters.
Click the Python Interpreter selector and choose Interpreter Settings. Click the Add Interpreter link next to the list of the available interpreters.
Select Add Local Interpreter.
Select Pipenv from the list of environment types.
Select the base interpreter from the list, or click and find the Python executable in your file system.
If you have added the base binary directory to your
PATH
environmental variable, you do not need to set any additional options: the path to the pipenv executable will be autodetected.If PyCharm does not detect the pipenv executable, click Install pipenv via pip to allow PyCharm to install it for you automatically.
Alternatively, follow the pipenv installation procedure to discover the executable path and then specify it in the dialog.
Click OK to complete the task.
note
If PyCharm displays the Invalid environment warning, it means that the specified Python binary cannot be found in the file system, or the Python version is not supported. Check the Python path and install a new version, if needed.
When you have set the pipenv virtual environment as a Python interpreter, all available packages are added from the source defined in Pipfile. The packages are installed, removed, and updated in the list of the packages through pipenv rather than through pip.
PyCharm can create a pipenv environment for your project based on the project requirements recorded in Pipfile.
When you open a project that contains Pipfile, but no project interpreter is configured, PyCharm suggests that you set up a pipenv environment.
PyCharm will display the path to the pipenv executable. Confirm the path by clicking OK.
If PyCharm cannot autodetect the pipenv executable, specify the path to it manually.
A new pipenv environment will be configured for your project and the packages listed in Pipfile will be installed.
note
PyCharm supports environment variables, for example,
PIPENV_VENV_IN_PROJECT
. You can addPIPENV_VENV_IN_PROJECT=true
to your ~/.profile, or ~/.bash_profile or ~/.bashrc and re-login to your OS in order for all the GUI processes to inherit new environment variables from your shell config.
For any of the configured Python interpreters (but Docker-based), you can:
Thanks for your feedback!