Create a Python module

It was unexpectedly troublesome to make it as easily as the node package system.

Creating a project

Create an appropriate directory.

mkdir mymodule && cd $_

Use virtualenv or direnv to isolate the system python from the python in the directory.

Virtualenv env for virtualenv For direnv, do direnv edit . and then layout python in the editor you opened. If you do pip freeze and only wsgiref appears, the environment construction is complete. It is isolated from the system pip, so you are free to pip install the required modules.

Directory structure

.
├── .gitignore
├── LICENSE
├── MANIFEST.in
├── README.md
├── mymodule.py
├── setup.py
└── tests
    └── myplugin_test.py

.gitignore

Based on Python's .gitignore, add the env directory if you use virtualenv, add .direnv if you use direnv. gitignore/Python.gitignore

README.md

Actually, restructuredText that is formatted and displayed with PyPI is better than markdown.

setup.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import with_statement
from setuptools import setup

with open("README.md") as f:
    long_description = f.read()

setup(
    name="mymodule",
    version="0.1.0",
    description="hogehoge.",
    long_description=long_description,
    author="yourid",
    author_email="youremail",
    url="yoururl",
    py_modules=["mymodule"],
    include_package_data=True,
    install_requires=["Flask"],
    tests_require=["nose"],
    license="MIT",
    keywords="",
    zip_safe=False,
    classifiers=[]
)

py_modules specifies the filename of the module to publish. install_requires and tests_require write out dependent modules. Depending on the article, pip freeze may be specified here, but it is better to write without manually specifying the version because it will be specified up to the version and it will be like dependency hell. Select classifiers from here.

MANIFEST.in

By default, only Python files are included in the module, so this is necessary to include special text files. This time, write ʻinclude README.md` to include README.md. If you prepare requirements.txt, you need to describe it here as well.

mymodule.py

Module body. Write appropriately.

tests/

Module testing. Write appropriately. Write properly.

test

Use nose. You can enter by doing pip install nose. Execution is nosetests

Register with PyPI and Test PyPI

Register with Test PyPI and PyPI. Create a .pypirc file in your home directory and write the registration information.

[distutils]
index-servers =
    pypi
    pypitest

[pypi]
repository: https://pypi.python.org/pypi
username: {{User name}}
password: {{password}}

[pypitest]
repository: https://testpypi.python.org/pypi
username: {{User name}}
password: {{password}}

Once the module is complete and the tests are complete, python setup.py register -r https://testpypi.python.org/pypi to register the package and then python setup.py sdist upload -r https: // Register with Test PyPI at testpypi.python.org/pypi. Since it is a test site, you can register and test more and more. If it is registered without any problem, you can install it with pip install --index-url https://testpypi.python.org/simple/mymodule. After testing whether it can be installed properly with Python on the system, import it and use it, etc., register it in the production PyPI.

Register modules on production PyPI with python setup.py register You can upload modules to production PyPI with python setup.py sdist upload.

end

I made this airtoxin/plugin-loader

Recommended Posts

Create a Python module
Create a Python environment
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
Create a python numpy array
Create a directory with python
Create a python GUI using tkinter
Create a DI Container in Python
Create a Python environment on Mac (2017/4)
Create a virtual environment with Python!
Create a binary file in Python
Use blender as a python module
Create a python environment on centos
Create a Python general-purpose decorator framework
Create a Kubernetes Operator in Python
5 Ways to Create a Python Chatbot
Create a random string in Python
Python module import
Python collections module
Create a new Python numerical calculation project
Create a dummy image with Python + PIL.
Create a python environment on your Mac
Create a simple GUI app in Python
Make a relation diagram of Python module
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create Python module [CarSensor API support module csapi]
Create a JSON object mapper in Python
Create a graph using the Sympy module
[Python] Create a Batch environment using AWS-CDK
[Python] [LINE Bot] Create a parrot return LINE Bot
Create a word frequency counter with Python 3.4
Create a deb file from a python package
[Python] Create a LineBot that runs regularly
[GPS] Create a kml file in Python
Create a compatibility judgment program with the random module of python.
Error when installing a module with Python pip
Create a frame with transparent background with tkinter [Python]
[Python] List Comprehension Various ways to create a list
Edit Excel from Python to create a PivotTable
A * algorithm (Python edition)
Create a Vim + Python test environment in 1 minute
Create a Django schedule
Create a GIF file using Pillow in Python
[Python] Take a screenshot
Create a C array from a Python> Excel sheet
[python] Create a list of various character types
[Python] Create multiple directories
A python lambda expression ...
Create SpatiaLite in Python
Create a LINE BOT with Minette for Python
I want to create a window in Python
Create a standard normal distribution graph in Python
python original module import
How to create a JSON file in Python
Create a virtual environment with conda in Python
Create a New Todoist Task from Python Script
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python