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:
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:
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 inpip3 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:
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:
pip3 install time_tracking_pkg-1.0.0-py3-none-any.whl
Once it done, we'll use pip3 list
to check if it install correctly.
Finally! We can run the command and get its result:
time-tracking-pkg
To see its location, use:
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 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.
Top comments (0)