Skip to content

Instantly share code, notes, and snippets.

@hhslepicka
Last active December 27, 2023 12:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hhslepicka/01d248be40bca3356c5d1fe9a6c2b288 to your computer and use it in GitHub Desktop.
Save hhslepicka/01d248be40bca3356c5d1fe9a6c2b288 to your computer and use it in GitHub Desktop.
BotCity - From Bot to EXE

Generating a Binary from a Bot Template

This short tutorial will demonstrate how to create an EXE file from a bot starting from the template.

Requirements

This short tutorial assumes that you already have the following dependencies installed. Note: it is recommended that you use a virtual environment.

  • Python 3.7 or greater
  • Git
  • Cookiecutter
  • virtualenv (recommended)

1. Installing the dependencies

Activate your virtual environment or use your current Python installation and do:

pip install pyinstaller

2. Create your Bot Project

Using the cookiecutter, create a simple Desktop Bot project:

cookiecutter https://github.com/botcity-dev/bot-python-template.git

After that you should now have a folder that matches your bot_id.

For the purposes of this tutorial I will assume the default value of botPython.

Please adjust to your selections.

3. Installing your Project

Every bot created with the template is a Python package that can be installed with:

pip install -e .

And executed with:

python -m botPython

Please note that the -e flag on pip install allow you to modify your code without the need to reinstall it each time something changes. It is called editable install (more info here).

4. Creating your EXE

Using pyinstaller we can now generate an EXE file for our Bot code. For that we need to create a simple python file with the following content:

from botPython.bot import Bot
Bot.main()

Save this file with the name my_exe.py

5. Making sure the Image Resources are accessible

Since pyinstaller --onefile makes it really just one big binary file, we need to make sure that it includes all of our resources from the botPython project.

For now that will require some additions to your code. Let's get to it.

5.1. Adding some code to your Bot

Open your bot.py file and add the following code to the class:

    def setup_images(self):
        # Add images manually to the map if we are using pyinstaller
        # pyinstaller injects the `_MEIPASS` variable into the `sys` module
        if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
            # Remember to change here from `botPython` to your bot ID.
            res_path = os.path.join(sys._MEIPASS, "botPython", "resources")
            # Add all images needed to the image map
            self.add_image("lupa", os.path.join(res_path, "lupa.png"))
            ...

After that you need to make sure to invoke this method in your action method like so:

    def action(self, execution=None):
        self.setup_images()
        ...

Here is the complete code on how the class will look like:

import os
import sys
from botcity.core import DesktopBot


class Bot(DesktopBot):
    def setup_images(self):
        # Adiciona imagens se pyinstaller
        if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
            res_path = os.path.join(sys._MEIPASS, "botPython", "resources")
            self.add_image("lupa", os.path.join(res_path, "lupa.png"))


    def action(self, execution=None):
        # Fetch the Activity ID from the task:
        # task = self.maestro.get_task(execution.task_id)
        # activity_id = task.activity_id
        self.setup_images()

        # Opens the Google website.
        self.browse("https://www.google.com")
        ...

6. Generating the EXE

Now we can generate the binary with the following command:

pyinstaller --onefile --collect-all botPython my_exe.py

Remember to change botPython to your bot ID.

This will take a while since it is processing the list of dependencies, generating the spec file and creating your binary. Once that is done, you should have a folder called dist and inside this folder your my_exe.EXE file ready to be executed.

If you double-click the file you should now see your default browser open the BotCity website.

Next Steps

Now that you are done with the basics, go ahead and modify your code as needed for your automation and to generate a new binary just simply repeate the command below:

pyinstaller --onefile --collect-all botPython my_exe.py
@RafaFerr
Copy link

Após a atualização do framework, é preciso ajustar algo no código acima para conseguir fazer o executável?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment