How to make a Python exe file automatically install dependancies?

5k views Asked by At

I have made an application using python using some libraries installed as needed on the go. Now I want to make it usable for person who doesn't know how to install dependencies and which ones are needed. What to do to transform it into an easy to use application and probably make it able to run on Mac too??

Please suggest some resources that might help me know more.

3

There are 3 answers

1
James On BEST ANSWER

When making the executable you should declare the modules with it, you can then just copy the library files to the same location as the programme.

I use cx_freeze when making exe's and an example setup.py files looks like this:

from cx_Freeze import setup, Executable

base = None    

executables = [Executable("projectname.py", base=base)]

packages = ["idna","os","shutil","csv","time","whatever else you like"]
options = {
    'build_exe': {    
        'packages':packages,
    },    
}

setup(
    name = "Some Project",
    options = options,
    version = "1.2.3",
    description = 'Generic Description',
    executables = executables
)

Packages are where the modules are declared.

If you have a quick search online it'll give you complete guides for it, all you'd need to do then is copy the lot across to your friend's computer.

Hope this helps!

0
wkokgit On

PyInstaller might be something you are looking for, which can create .exe files from python scripts.

Here's the documentation. Be aware that the __import__() function with variable data isn't detected by PyInstaller, so you should just use import ..., and then PyInstaller will add the dependencies so that they are used in the .exe file.

0
AudioBubble On

As Wouter K mentioned, you should install Pyinstaller (pip install pyinstaller for pip) and then cd to the directory you want and type pyinstaller --onefile file.py on the terminal. If you cded in the directory your file is, you just type the name and the extension (.py) of your file. Else, you will have to specify the full path of the file. Also, you can't make an mac os executable from a non mac os pc. You will need to do what I mentioned above on a mac.