The Startup
Published in

The Startup

Configure PyCharm With the Python Virtual Environment

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

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Scrum way of Feature Development: Zomato app use case

Raptor! My new tool for web automation

Progressive Rollout Strategy on Trendyol Android App

Surprises from Polling Kinesis

Effective application analytic

MAGE tutorial — 03. Data validation

How to prepare your company for a sales-season — from a technological perspective.

Quickly Debug Your AWS Lambda Functions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gioacchino Lonardo

Gioacchino Lonardo

Computer Engineer, artificial intelligence enthusiast.

More from Medium

Ask For Help If You Need It

IS THE DIVINE AN EXPERIENCE THAT EXCEEDS HUMAN THOUGHT AND BELIEF ?

Why I Hate Being Called an Instagrammer by chicute

ASOS Classic Retro Sunglasses - Why I Hate Being Called an Instagrammer by popular DC blogger Alicia Tenise

EARTHIAN HUMANS EXPERIENCE LESS OKNESS