Site icon AranaCorp

Best practices for creating a Python Project

In this tutorial, we’ll look at the best practices for arranging a Python project so that it can be shared and deployed.

Whether it’s software, a library, a web app or a mobile app, if you want to share your work, you need to organize and document it properly.

You can apply these practices to the Serial Monitor project

Create a virtual environment

When you want to create a project that you’re going to share and maintain over the long term, it’s essential to integrate it into a virtual environment to isolate its dependencies (libraries used, versions, etc.).

Under Windows, make sure that the version of python you are using can be called with python.exe

N.B.: Under Windows, to open the console at the desired location, you can create a start_console.bat file into which you copy the command

start cmd.exe

You can run the file by double-clicking to open the terminal.

Once the environment has been created, you can add or remove packages.

pip install <package_name>
pip install <package_name>==<version>
pip uninstall <package_name>

And keep track of the packages and versions used in the requirements.txt file.

pip freeze > requirements.txt

Python project file architecture

Based on Kenneth Reitz’s recommendations, here’s the file structure to adopt.

helloworld/
│
├── .gitignore
├── helloworld.py
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py
└── tests.py
sample
│
├── docs
│   ├── conf.py
│   ├── index.rst
├── sample
│   ├── __init__.py
│   ├── core.py
│   ├── helpers.py
├── tests
│   ├── test_basic.py
│   ├── test_advanced.py
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py

N.B.: To display your folder tree, you can use the command tree

In practice, you’re free to create your own folder architecture. The important thing is that it’s functional, modular and organized.

Here is a description of the essential architecture files

Once you’ve set up this architecture, you can use and adapt it for all your Python projects.

Sources

Exit mobile version