Last Updated:

Creating virtual environments in Python

For Python 3.3+, the built-in venv module is used, rather than the third-party virtualenv utility.

Create a virtual environment

  1. Make sure you download and install Python on your computer. (If you don't have Python yet, you can download it from the official website: https://www.python.org/downloads/
  2. Press Ctrl+Alt+S to open Project Settings.
  3. In the Settings/Preferences Ctrl+Alt+S dialog box, select Project <project name> | Python interpreter. Click the Configure Project Interpreter icon, and then click Add.
    Creating virtual environments in Python
  4. In the left pane of the Add Python Interpreter dialog box, select VirtualenvEnvironment. The following steps depend on whether the virtual environment previously existed.
    Creating virtual environments in Python

If a new environment is selected:

  1. Specify the location of the new virtual environment in the text box, or click Virtual Environment Location and locate the location in the file system. Note that the directory where the new virtual environment should reside must be empty!
  2. Select a base interpreter from the list, or click Select Base Interpreter and locate the Python executable in your file system. If PyCharm doesn't detect Python on your computer, it provides two options: download the latest versions of Python from your computer. python.org or specify the path to the Python executable (in the case of a custom installation).
    Creating virtual environments in Python
  3. Select the Inheritglobalsite-packages check box if you want to inherit your global site packages directory. This check box corresponds to the —system-site-packages parameter of the virtualenv tool.
  4. If necessary, select the Make available to all projects check box.

If you selected an existing environment:

  1. Expand the list of translators and select any of the existing translators. Alternatively, click the Choose Interpreter button and specify the path to the Python executable file in your file system, for example: C:\Python36\python.exe.
  2. Select the Make all projects available if necessary check box.
  3. Click OK to complete the task.

You can create as many virtual environments as you need. To easily distinguish them from each other, use different names.

Set up an existing virtual environment

  1. Open the Settings/Preferences dialog box by pressing Ctrl+Alt+S or by selecting File | Settings for Windows and Linux or PyCharm | Settings for macOS. Select Project<project name> | Python interpreter.
  2. Expand the list of available translators and click the Show All link. Also, click the Customize Project Interpreter icon and choose Show All. Virtual environments are marked as Virtual Environment.
    Creating virtual environments in Python
  3. Select the target environment from the list, and then click OK to confirm your selection. PyCharm can create a virtual environment for your project based on the project requirements.
    Creating virtual environments in Python

Create a virtual environment using project requirements

  1. Open any source directory that contains requirements.txt or setup.py file: select File | Open from the main menu and select a directory.
  2. If you have not created a virtual environment for this project, PyCharm prompts you to create one:
    Creating virtual environments in Python

    Save the suggested settings or specify the location of the environment or the underlying Python interpreter. Click OK to complete the task. As soon as you click OK, PyCharm creates the environment and installs all the necessary packages.

When you're done, see the notification pop-up. Note that if you ignore the prompt to create a virtual environment, PyCharm will not create a Python interperter for your project. That way, every time you open a .py file, you'll see a warning with project interpreter settings.