This short tutorial will demonstrate how to create an EXE file from a bot starting from the template.
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)
Activate your virtual environment or use your current Python installation and do:
pip install pyinstaller
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.
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).
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
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.
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")
...
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.
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
Após a atualização do framework, é preciso ajustar algo no código acima para conseguir fazer o executável?