Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python compile all modules into a single python file

Tags:

python

I am writing a program in python to be sent to other people, who are running the same python version, however these some 3rd party modules that need to be installed to use it.

Is there a way to compile into a .pyc (I only say pyc because its a python compiled file) that has the all the dependant modules inside it as well?

So they can run the programme without needing to install the modules separately?

Edit: Sorry if it wasnt clear, but I am aware of things such as cx_freeze etc but what im trying to is just a single python file.

So they can just type "python myapp.py" and then it will run. No installation of anything. As if all the module codes are in my .py file.

like image 682
binarysmacker Avatar asked Sep 14 '26 01:09

binarysmacker


1 Answers

If you are on python 2.3 or later and your dependencies are pure python:

If you don't want to go the setuptools or distutiles routes, you can provide a zip file with the pycs for your code and all of its dependencies. You will have to do a little work to make any complex pathing inside the zip file available (if the dependencies are just lying around at the root of the zip this is not necessary. Then just add the zip location to your path and it should work just as if the dependencies files has been installed.

If your dependencies include .pyds or other binary dependencies you'll probably have to fall back on distutils.

like image 161
theodox Avatar answered Sep 15 '26 14:09

theodox