How to package and distribute Python scripts


I made a script to operate Trello from the command line in Python, but I wanted to execute the same command not only on the local Mac but also on EC2 dedicated to work, so this article I referred to items / 803923b2ff02482242cd) and investigated and summarized the script packaging and distribution method.


Directory structure (STEP1)

This is the initial state of implementing the script without considering distribution.

|__ trello_cli
    ├── #Main script
    |__ #File called by command

The first directory structure looks like this, the command execution is

$ cd trello-cli
$ python -m trello-cli 

And you need to change to the trello-cli directory. This command also runs

The contents of \ _ \ _ main \ _ \ _. Py look like this, just calling the main () function in \ _ \ _ init \ _ \ _. Py. The actual processing is implemented in the main () function of \ _ \ _ init \ _ \ _. Py.

from . import main


Manage dependent packages (STEP2)

Save what you need to install pip to use this script.

$ pip freeze > requirements.txt

In my case, the contents of the file look like this. It's written in various ways, but all I needed was py-trello and python-dotenv.



At this point, the directory structure looks like this:

├── requirements.txt
|__ trello_cli

Write (STEP3)

import setuptools

with open("", "r") as fh:
    long_description =

        'console_scripts': ['trello=trello_cli:main'],
    author_email="[email protected]",
    description="Trello card managment on CLI",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",

--If you write a package that depends on the package in install_requires, pip will install it automatically. --The contents specified in console_scripts of entry_points can be executed as a command on the console. In this case, the trello command runs trello-cli / __

At this point, the directory structure looks like this:

├── requirements.txt
|__ trello_cli

Make a handout (STEP4)

Finally, this script is ready for pip install.

$ python bdist_wheel

Then, as shown in the directory configuration diagram below, a directory called dist will be created, and a file for pip install will be created in that directory.

├── dist
│   └── Trello_CLI-0.0.1-py3-none-any.whl
├── requirements.txt
|__ trello_cli

Actually use it on EC2

Since the purpose of this time is to use the same command as local on EC2, I will install it on EC2 immediately using the created distribution. First, even if you use a virtual environment, python and pip must be the same version as local.

$ cd trello-cli
$ pip install ./trello-cli/dist/Trello_CLI-0.0.1-py3-none-any.whl
$ trello

I succeeded in executing the command as if it were local.


For things that I do not want to write in the source code such as Trello's API Key, I used python-dotenv and described it in the .env file, but of course I will prepare the .env file at the installation destination as well. Otherwise, an error will occur.

The location of the extracted directory changes depending on the situation where python is running, but in my case it is running with anaconda, so~ / anaconda3 / lib / python3.8 / site I had to create a.env file in a ridiculously confusing place called -packages / trello_cli / .env.

If anyone knows how to make it smoother, I would appreciate it if you could comment.


Recommended Posts

How to package and distribute Python scripts
How to execute external shell scripts and commands in python
How to install and use pandas_datareader [Python]
[Python] Package and distribute your own modules
python: How to use locals () and globals ()
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
How to use is and == in Python
How to install Python
How to install python
How to generate permutations in Python and C ++
[Python] How to read data from CIFAR-10 and CIFAR-100
How to convert SVG to PDF and PNG [Python]
[Python] How to use hash function and tuple.
How to plot autocorrelation and partial autocorrelation in python
How to write the correct shebang in Perl, Python and Ruby scripts
[2020.8 latest] How to install Python
[Python] [Django] How to use ChoiceField and how to add options
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
How to make a Python package using VS Code
[Python] How to sort dict in list and instance in list
[Python] How to use checkio
How to run Notepad ++ Python
[Python] How to split and modularize files (simple, example)
[Python] How to create Correlation Matrix and Heat Map
How to change Python version
How to develop in Python
Python # How to check type and type for super beginners
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to swap elements in an array in Python, and how to reverse an array.
[python] Summary of how to retrieve lists and dictionary elements
[Python] How to play with class variables with decorator and metaclass
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Python] How to set variable names dynamically and speed comparison
How to learn TensorFlow for liberal arts and Python beginners
How to make a Python package (written for an intern)
[Python] Summary of how to use split and join functions
A standard way to develop and distribute packages in Python
How to do Bulk Update with PyMySQL and notes [Python]
Comparison of how to use higher-order functions in Python 2 and 3
How to convert Youtube to mp3 and download it super-safely [Python]
[Blender] How to handle mouse and keyboard events in Blender scripts
How to write a metaclass that supports both python2 and python3
How to log in to AtCoder with Python and submit automatically
Overview of Python virtual environment and how to create it
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with