How to package and distribute Python scripts

Introduction

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.

environment

Directory structure (STEP1)

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

trello-cli
|__ trello_cli
    ├── __init__.py #Main script
    |__ __main__.py #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 main.py.

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.

__main__.py


from . import main

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.

requirements.txt


certifi==2020.6.20
chardet==3.0.4
idna==2.10
oauthlib==3.1.0
py-trello==0.17.1
python-dateutil==2.8.1
python-dotenv==0.15.0
pytz==2020.4
requests==2.24.0
requests-oauthlib==1.3.0
six==1.15.0
urllib3==1.25.11

At this point, the directory structure looks like this:

trello-cli
├── requirements.txt
|__ trello_cli
    |__ __init__.py
    |__ __main__.py

Write setup.py (STEP3)

setup.py


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="Trello-CLI",
    version="0.0.1",
    install_requires=[
        "requests",
    ],
    entry_points={
        'console_scripts': ['trello=trello_cli:main'],
    },
    author="Yokohama",
    author_email="[email protected]",
    description="Trello card managment on CLI",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.8',
)

--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 / __ main__.py.

At this point, the directory structure looks like this:

trello-cli
├── requirements.txt
├── setup.py
|__ trello_cli
    |__ __init__.py
    |__ __main__.py

Make a handout (STEP4)

Finally, this script is ready for pip install.

$ python setup.py 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.

trello-cli
├── dist
│   └── Trello_CLI-0.0.1-py3-none-any.whl
├── requirements.txt
├── setup.py
|__ trello_cli
    |__ __init__.py
    |__ __main__.py

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.

Task

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.

reference

https://qiita.com/propella/items/803923b2ff02482242cd

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