How can I distribute the . py program without the user having to install all libraries?

Asked

Viewed 3,572 times

23

I’m learning Python and I need to distribute a program, I read about cx_Freeze and py2exe to generate one .exe. However, I don’t mind distributing the program code together, so I don’t see the need to generate one. exe.

How can I distribute the . py program without the user having to install all the libraries used in the program? I can create an installer?

  • 2

    It would be interesting to open another question regarding password protection, even if it is the distribution process and python, so you can have help from more people and more direct answers.

  • 2

    I’ve already taken the last question.

4 answers

11

Well, I don’t know if this is possible, but maybe you can do this to facilitate the installation of packages:

Using a file .bat you can install the libraries using Pip, for example:

start /w pip install numpy 
start /w pip install matplotlib
start /w pip install qualquerOutraBiblioteca
...

If you need to put the Pip in the system path, before the above code, put:

SETX PATH "%PATH%;C:\Python33\Scripts"

If you need to install Pip, put it first:

start /w python get-pip.py

Link to get-Pip.py

About the Pip.


If you want to generate an installer .msi, take a look at pynsist.

  • Note that this approach however restricts distribution to Windows systems. The process I describe below generates a complete installer for Windows, but it can output to other platforms as well. The process that requires manual interaction, with the creation of the file "Requirements.txt" is also cross-platform.

9

Python has a package management ecosystem (the libraries) integrated both in the language and in its own packages.

PIP - a much needed tool that has become part of the new Python installations since Python 3.3 solves manually the question of dependencies for those who are in a hurry and do not want to leave the round package to third parties.

Of course, all of this assumes that your project is already running in a separate virtualenv, where the only libraries installed besides your own project are the ones you want to ensure are installed in the destination.

If you are not already using Virtualenv for its development, take a look here: http://docs.python-guide.org/en/latest/dev/virtualenvs/ (or, for Python >= 3.3 venv, which comes with Python): https://docs.python.org/3/library/venv.html

In this case, you can use PIP to create a file with the list of Python libraries installed in your project - type: pip freeze >requirements.txt in the terminal, with the virtualenv activated. This will automatically list the installed libraries in the "Reset.txt" file-- whoever installs your program, after having the code in hand, and creating virtualenv, must type: pip -r requirements.txt.

Python developers used to take and place projects on sites like github will know how to install packages and libraries following this recommendation.

But, that doesn’t answer your question - if you want a layman to be able to install your program, having only Python installed, that’s possible yes - but it takes a little more work to get it right the first time, for the developer.

You will then make use of "setuptools" - a Python package made for, from information about your project that you put in a filename setup.py - This file is a small Python program where you put not only the dependencies of your project, (such as module build instructions, if there is part of the project in C or Cython), metadata about the version number of the project, author, and license of use and so on.

The setuptools makes the setup.py function as a full command line application, with several options. One of them is even calling direct python setup.py bdist_wininst - that in a properly configured Windows environment will generate a file. EXE that installs your application and its requirements (only does not install Python - as pyfreeze and other utilities do).

Now setuptools is used, more normally, not to create installables for Windows - and yes - to put your project, once tested and fulfilling some quality requirements, right in Pypi - the "Python Package Index". From there, anyone with Python and Pip (or easy_install) installed will be able to install your project simply by typing: "Pip install " (and, yes, this installs your project and the libraries it depends on). For programmers and Python users it turns out to be more practical than having an installer on some web site - you don’t have to find the site, download the file, and run it - just type the command. (And also your project becomes part of the list of projects that may be requirements of others).

For the lay public who will install, for example, a game made with Pygame, the form with Windows installer is certainly more convenient.

The setuptools documentation is something that is really worth studying if you want to distribute your projects to the general public, whether end users or even the Python programmer community. Start with the link: https://pypi.python.org/pypi/setuptools

2

You can use the module zipapp in versions 3.5+.

TL;DR

Let’s assume that I will make an application that queries a user API. For this we will need the package requests.

We will have the application directory:

⊦ users/
    ⊦ venv/
    ⊦ users/
        ⊦ app.py
    ⊦ requirements.txt

For this, we create the directory:

$ mkdir users %% cd $_

We start a virtual environment:

$ python3.6 -m venv venv
$ source venv/bin/activate

We installed the package requests:

$ pip install requests

And generate the file requirements.txt:

$ pip freeze > requirements.txt

So we can write our application. For example, I will use the API https://reqres.in/api/users and we will ask the user which page of users they wish to list. Thus, our code will be:

# users/app.py

import requests

API = 'https://reqres.in/api/users'

def main():
    while True:
        page = input('Digite o número da página: ')
        params = {'page': page}
        response = requests.get(API, params=params)

        if response.ok:
            data = response.json()
            if data['data']:
                for user in data['data']:
                    print(f'#{user["id"]}: {user["first_name"]} {user["last_name"]}')

if __name__ == '__main__':
    main()

We can test it locally by making:

$ python users/app.py
Digite o número da página: 1
#1: George Bluth
#2: Janet Weaver
#3: Emma Wong

Perfect. Working. Now we want to package this our application to share with other users.

First, we install the dependencies of the application directly in the directory itself, so that it is self-contained. For this, we just run:

$ pip install -r requirements.txt --target users

With the dependencies installed locally we can create the distribution file from the package zipapp:

$ python -m zipapp -m 'app:main' users

This will create the file users.pyz, which can be run directly from Python and already has all the dependencies of the application. That is, just send this file to the other users and they run:

$ python users.pyz
Digite o número da página: 1
#1: George Bluth
#2: Janet Weaver
#3: Emma Wong
Digite o número da página: 2
#4: Eve Holt
#5: Charles Morris
#6: Tracey Ramos

The option -m of zipapp will define the application’s input port, so we define it as app:main, that is, module app.py, function main.

But remember that this will only package your application and its dependencies within a compressed file. This will consequently leave the file larger and will not overshadow the source code, so much so that it is possible to open the file directly users.pyz:

inserir a descrição da imagem aqui

0

A way to distribute packages in Python very similar to what java does with files .jar sane Eggs. This way the user can run his program with:

$ python program.egg

Do not worry about dependencies, which can be bundled with your program. I recommend looking for a reading on how to create Eggs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.