Webinar
Switchable Models, Personalization, and Onboarding Agent
Home / Blog /
How to configure PyCharm venv
//

How to configure PyCharm venv

//
Tabnine Team /
7 minutes /
August 28, 2021

PyCharm is the go-to IDE for Pythonists, no matter what the Python project is. From Django to Flask, PyCharm’s extensive support makes it an attractive out-of-the-box solution.

venv – or “virtual environments” – is a Python module that’s used to create a lightweight and isolated environment for running Python projects. It’s used to keep the dependencies required by different projects isolated from one another.

To create your own venv in Python, the general flow of command you would need to enter looks something like this:

python3 -m venv /path/to/new/virtual/environment

If you need help, run venv with the -h flag to get the list of available commands.

usage: venv [-h] [–system-site-packages] [–symlinks | –copies] [–clear]
            [–upgrade] [–without-pip] [–prompt PROMPT] [–upgrade-deps]
            ENV_DIR [ENV_DIR …]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

optional arguments:
  -h, –help            show this help message and exit
  –system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  –symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
  –copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  –clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
  –upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  –without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)
  –prompt PROMPT       Provides an alternative prompt prefix for this
                        environment.
  –upgrade-deps        Upgrade core dependencies: pip setuptools to the
                        latest version in PyPI

Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.

This is a good starting point and great for when you’re building a Python project from scratch. However, integrating it into your IDE’s workflow can require a bit of setup. In addition to this, different project types require different configurations of the venv. Fortunately, PyCharm comes with an easy-to-use integrated solution that makes managing dependencies easy and painless.

How to configure a virtual environment in PyCharm

To create a virtual environment in PyCharm, you need to have Python installed on your computer.

To do this, go to the official Python download page and select the download package that is suitable for your specific operating system. Follow through with the OS-specific instructions to install Python on your machine.

If you are using Python 3.3+, you can skip Part 1 because the built-in venv module is used. However, if you are using a lower version of Python, here is how you can use venv for your project.

Is your knowledge of software development up-to-date?

It’s important to ensure that you’re knowledgeable about the most recent advancements and trends in software development. Staying informed about new technologies and practices will equip you with the necessary knowledge and abilities to excel in this constantly evolving industry. Discover how to utilize AI for optimizing your software engineering in 2023.

Part 1: Downloading and installing venv

To set up and configure a virtual environment on PyCharm, head over to the status bar located at the bottom of your IDE. Click on the option Python Packages.

This will give you a popup screen that looks something like this:

In the search bar, look up virtualenv. The search panel will give you the virtualenv package for you to download and install. Click on the install button in the top right-hand corner.

Part 2: Setting up your project’s virtual environment in PyCharm

Head over to your top navigation bar, click on File, and then Settings. In the left-hand panel, open up the drop-down that says Project:<yourprojectnamehere>. There will be an option called Python Interpreter.

Click on the little settings cog and select the Add option.

You will get a screen that looks like this:

If you are running lower than Python 3.3, you will need to select the dropdown for Base interpreter and find your downloaded venv package. If you are running Python 3.3+, then the option of your base interpreter is automatically chosen for you.

If this is an existing project and you want to import a pre-existing interpreter, you can do so by selecting Existing environment and then changing the Interpreter field to your settings.

Once you’ve done that, click on OK to create a new environment. You will get a progress bar in PyCharm’s IDE that updates you on the creation of the virtual environment.

Once completed, you will get a blank virtual environment with only pip and setuptools . 

Part 3: Installing dependencies and packages

Every project requires some sort of dependency and packages to work. You have two options in PyCharm when it comes to installing dependencies and packages for your Python project’s virtual environment.

To do this, you can do so via the + sign in the Python Interpreter section, or via the Python Packages option at the bottom bar of your main IDE’s view.

Option 1 – Installing dependencies and packages for venv via  PyCharm Python Interpreter

Clicking on the + sign inside the Python Interpreter will give you a screen of Available Packages. You can use the search bar to find the package and dependency you need for your particular project. For example, if you’re building a Django project, you can search up Django and it will give you the latest option available. 

Click on Install Package to install the package as part of your venv requirements. When you click on Install Package, you will get an update on the screen to tell you that it is installing.

Once it is completed, you will get a green notification bar at the bottom of your Available Packages view.

To uninstall any packages you do not need or want, you can do so via the minus – sign on the main Python Interpreter screen.

Once it has successfully uninstalled the package from your venv, you will get a green success message at the bottom of the view.

Option 2 – Installing dependencies and packages for venv via Python Packages

Look at the bottom of your PyCharm IDE and select the option Python Packages. This will give you a popup of the currently installed packages.

In the search bar, look up the package you want to install. Here’s an example of how to install Django.

Click on the install button to add it to your packages and dependencies list. The install button will change to a loading bar. This will disappear once you’ve successfully installed the package to your virtual environment as a dependency.

Once completed, if you click on the x in the search bar, you will see all the packages that are in your Python project’s venv.

To uninstall the package from your Python’s virtual environment and remove it as a dependency, select the package you want and select on the triple dots option to your right. Click on delete.

Once it has been deleted from your project’s dependencies, the package will be removed from the list.

Wrap up

Setting up and managing your virtual environment in Python with venv is pretty simple in PyCharm. There are no terminal commands needed. It is great for both beginners and seasoned developers looking to supercharge their workflow, especially with PyCharm.