create compiled version of python source

Viewed 191

I have a scraping project and to run the project using command

python3 source/index.py

index.py is the entry point to scrape the web

Now I need to make cron job on gcp compute engine...

Is this possible to create a cron job without actually putting the source code there, something like compile version of project.

Thanks

2 Answers

You can use pyinstaller from the PyPi repository, which converts your python code to a binary executable and supports multiple platforms.

You can find a simple tutorial at https://techmonger.github.io/82/pyinstaller-script-to-binary. You can also find full documentation here, which you should read.

To compile full source, try this command

pyinstaller --onefile yourEntrySourcecode.py

You can compile Python scripts to a binary code using various methods, but I have found out that using Nuitka is more efficient.

Nuitka is a Python-to-C++ compiler that supports almost all versions of python. for optimization you can choose the compiler that is going to use for the compilation

for more information go into the official documentation

The command syntax is as easy as

$ nuitka --standalone --recurse-all main.py

you can also use pyinstaller with some options to compile all the project.

$ pyinstaller --onefile main.py

But the program compilation is not neccessary in fact crontab can run any shell command in the 6th field separated by %.

here are the fields

minutes hours week_day(0-6) month command

Example cron job that will execute every 2 minutes:

2 * * * /usr/bin/python3%/path/to/source/index.py

Useful links:

Related