edhouse-CookieGdpr-Policy-s
4313657
2
/en/gdpr/
532650B6A

Back to Blog

TutorialsSQA

How to Start a Python Project

Tech_blog

You’ve probably encountered Python in software testing before, and you may even be using it in your project. But do you know how to properly start a project and create a virtual environment? This article explains how to keep your code organized and avoid breaking your project by accident.

Python Installation

Installing Python is easy, all you have to do is download the installation package from the official website and follow the instructions.

I believe that the installation does not need to be described in any special way. However, you should pay attention to the path of installation and the option to add the Python interpreter (python.exe) to the PATH system variable. In order to set up the development environment, we will need the path where Python will be installed.

Consider adding it to the PATH – personally, I would recommend not adding python.exe to the PATH. You may have multiple versions installed on one machine and adding it to the PATH can make things more complicated.

Installation verification

You can verify that you have Python installed correctly by listing its version using the command line command python.exe -V:

Create a project and set up the interpreter

Python does not strictly require a hierarchy of folders and files – you can have all your scripts “stacked” in one folder. However, I would still recommend keeping things in order and following at least the one project = one folder rule.

If you are starting a new project, create a new folder for it. I also recommend using Git right from the start.

Open the folder (project) in your favorite editor. I use VS Code, but the process is very similar in other editors.

Python is an interpreted language – in simple terms, this means that you pass your scripts (*.py files) to an interpreter (python.exe), which creates machine code from them and performs the operations.

When you create the first Python file in your project, you will need to set up the interpreter to run it. If you’ve set it up in the past, it’s probably set up automatically now. If you have multiple versions installed on your computer, you can choose which one to use for your project.

Virtual environment

The thing that most beginners forget or don’t know about is setting up a virtual environment. This allows you to have a Python environment in your project that is separated from the system one. You may freely install packages, do “high jinks”, and you don’t have to worry about messing up or making your Python unusable by installing packages.

Create a new environment by using the command line in your project folder. For example, I named the interface “myvenv“, but you can use any name you like:

python.exe -m venv myvenv

VS Code recognizes that a virtual environment is created in the project and you can thus assign it to the project directly.

You may notice that the interpreter has also changed – instead of the main (system) python.exe, we now use the one stored in the newly created virtual interface.

You can have as many environments as you want in a single project. So you can create a new environment in which you explore different packages, and then only install the one that is right for you into your main environment.

Activation and Use of the environment

The environment still needs to be activated using the script “Activate”:

.\myvenv\Scripts\activate

You can tell that the environment has been activated by its name at the beginning of each new line in the command line.

Installation of packages and pipreqs

Now you can start programming your project and install packages using the pip install command. If you have an environment activated, packages will only be installed in that environment. As an example, I installed pytest in my environment.

In the following picture you can see that the installation in the environment was successful, and I can use it (top window), but the system environment was left untouched (bottom window):

After activating the environment, I recommend installing the pipreqs package. This will allow you to attach a file to your project that lists the packages you are using:

python.exe -m pip install pipreqs 

Running pipreqs --force in the project folder will generate a requirements.txt file that contains the names and versions of the packages you are using in the project:

Pipreqs is not the only package that allows you to manage packages in your environment in this manner. It is also possible to create a requirements.txt file using pip freeze, but this will list all installed packages, including defaults and unneeded ones.

Code Sharing and Git

What is the purpose of creating a requirements.txt file? The environment directory (in this case /myvenv) doesn’t belong in the repository, but at the same time you want to give other developers the ability to use the project on their own systems. The requirements.txt file will be their guide to what packages they need to install for your project to run successfully.

.gitignore

You can specify files and folders that you don’t want to put in the git repository in the .gitignore file. In our case, we’ll add our environment folder first. But we can also add some other folders and files. You can look at the Github template for examples and usage.

Install packages using requirements.txt

Anyone who subsequently downloads your project from Git can simply install the necessary packages using PIP:

pip install -r requirements.txt

Let´s do it

I hope this article will help you to start your next Python project. Knowledge about proper setup and use of a virtual environments will help you better manage your packages and keep your project in order.

Go ahead and feel free to share your experiences in the comments.

Share article

Author

Jan Zatloukal

Jan ZatloukalTester and developer with a passion for automation and improving the development process. I am currently working on an electron microscope automation project in Python.

Edhouse newsletter

Get the latest updates from the world of Edhouse – news, events, and current software and hardware trends.

By signing up, you agree to our Privacy Policy.

Thank you! You have successfully subscribed to the newsletter.