How I Made My First Linux Distribution Package
Tags: python, linux, tutorial
Published: 2023-08-22 19:26:32

I built Linux software or distribution packages using Python. I accomplished this with the assistance of `wheel`, which compiles my source code into a single installation file compatible with any Linux system.

We'll begin with a simple Python code named `main.py`, which will print out the current time every 5 seconds in an infinite loop.

Ensure that you have a directory structure like the following (this is necessary for proper functioning): a root directory, followed by the package name directory and its module files.

> time-tracking-package/
> --time_tracking/
> ----main_file.py
> --setup.py

Our `main_file.py` code:
```python
import time


def main_func():
    try:
        while True:
            current_time = time.strftime("%Y-%m-%d %H:%M:%S")
            print("Current time:", current_time)
            time.sleep(5)
    except KeyboardInterrupt:
        pass


if __name__ == '__main__':
    main_func()
```

Next, we'll add the Python configuration file setup.py to compile the entire project into an installable file. This file will be used for installation on the Linux server:
```python
from setuptools import setup


setup(
    name='time-tracking-pkg',
    version='1.0.0',
    install_requires=[
        'wheel',
    ],
    entry_points={
        'console_scripts': [
            'time-tracking-pkg=time_tracking.main_file:main_func'
        ]
    },
)
```

- `name` is the package name in `pip3 list`. In case you use underscore "_", the package name will still convert it to dash "-" anyway.
- `version` of the package.
- `install_requires` the requires package which will be install during the setup.
- `console_scripts` is where we define the command of the package(time-tracking-pkg) and its source(time_tracking). The package has to be identical to the package name, you can use either "-" or "_" on the command unlike package name.

Compile our project by: 
```command
python3 setup.py bdist_wheel
```

We'll get 3 newly created directories: `dist/`, `build/` and `time_tracking_pkg.egg-info/`. 

We'll use the file `time_tracking_pkg-1.0.0-py3-none-any.whl` located in `dist/` to upload it to the Linux Server(Ubuntu). Also make sure that you have python3 and pip3 on your Ubuntu Server. 

After uploading to Ubuntu Server, use this command to install the package into our server: 
```command
pip3 install time_tracking_pkg-1.0.0-py3-none-any.whl
```

![Installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icmsqfj6wi0dl0ei5a4m.png)

Once it done, we'll use `pip3 list` to check if it install correctly.

![Checking](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qiuzbzwnmhgqkbwo3fm5.png)

Finally! We can run the command and get its result:
```command
time-tracking-pkg
```

![Run](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kae7uc2bcxnfpcqt2dsb.png)

To see its location, use:
```command
pip3 show -f time-tracking-pkg
```

## **Conclusion:**
You can also add another package in this project, which will be included in the setup file. But please keep in mind that both package will be remove when you uninstall with:
```command
pip3 uninstall time-tracking-pkg
```

In the next article I'm going to show you how I made it run on the server and we will adjust the code in order to check how it works.

I hope this article is helpful to you. I struggled when I attempted to develop it for my work, which is why I decided to write about it here.


<br />
<u>Looking for qualify software with affordable price? contact: funwarn.dev@gmail.com</u>