Configure PyCharm With the Python Virtual Environment

Gioacchino Lonardo
The Startup
Published in
4 min readSep 26, 2020

--

It will show you how to configure PyCharm to work with Python in a virtual environment. Also you will configure the requirements.txt.

Start PyCharm create a new project or clone it from your repository. As an example I created a file first_example.py to write inside the main. I specify that this is only an example and for reasons of brevity does not follow the structure of a Python project.

Install Python

First, make sure Python 3 is installed by running the following command:

python --version
python3 --version

If python 2 and 3 is installed, the output will be the installed version of python 2 and 3.

If python is not installed, run the command and choose version 3.6 for example¹:

sudo apt-get install python3.6
sudo apt install python3-pip python3-venv

Create virtualenv with:

python3 -m venv <venv-directory>

It is possible to create virtualenv on the same directory or on a different directory. In this article I use the same directory as the project directory. In my case i call the directory “venv”, but we can use “any_name”:

If all went well you will have the folder venv in your project:

To activate virtual environment lunch:

The fact that there is venv in parenthesis means that all libraries will be installed in the virtual environment and will not “dirty” the global environment. For more information about the virtual environment read this https://docs.python.org/3/tutorial/venv.html.

Python Interpreter

If you can’t find the python interpreter in the previous window then you have to create or add it. Just go to File →Settings →Project →Python interpreter →Add.

In the Interpreter box is present the path of our virtual environment:

Click Ok and then on Apply, then we will see all the libraries installed in our virtual environment.

Executing a program in the virtual environment

Let’s consider the example first_example, go to Add Configuration…

A window will open. Click on the “+” in the upper left corner. Now we can configure our Python program ready to run. Enter the “Name” (first_example), set “Script path” to the file where the main is located. Then we should find the “Python interpreter” already configured, in any case, having added it we will find it in the dropdown menu. Note that the interpreter path is the one related to the virtual environment created!

Lunch simple program

Let’s consider the following code:

the result:

requirements.txt

If you share a project with other users using a build system or plan to copy the project to another location where an environment is to be restored, you must specify the external packages required by the project. The recommended approach is to use a requirements.txt file, which contains a list of pip commands and allows you to install the required versions of dependent packages.

We assume to install Pandas (data analysis and manipulation tool), we lunch:

pip3 install pandas

In this case version 1.1.2 of pandas has been installed since we have not expressed a preference on the version. Because we will develop several things with this version, some methods may become deprecated, so to save the installed version, we create the requirements.txt file in the root directory of the project.

You can install the libraries specify in the requirements.txt file with the command:

pip install -r requirements.txt (Python 2)
pip3 install -r requirements.txt (Python 3)

References

[1] Installing Python 3 on Linux — https://docs.python-guide.org/starting/install3/linux/

[2] Virtual Environments and Packages — https://docs.python.org/3/tutorial/venv.html

--

--

Gioacchino Lonardo
The Startup

Computer Engineer, AI enthusiast, biker, guitar&bass player