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.
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()
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
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
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
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.
https://qiita.com/propella/items/803923b2ff02482242cd
Recommended Posts