Skip to content
Snippets Groups Projects
Unverified Commit d44b3a74 authored by Laurent Modolo's avatar Laurent Modolo
Browse files

pratical first part

parents
No related branches found
No related tags found
No related merge requests found
README.html: README.md
pandoc -s --highlight-style pygments --mathjax README.md -o REAME.html
README.md 0 → 100644
---
title: Packaging with Python
---
## Python `pip`
[![cc_by_sa](./img/cc_by_sa.png)](http://creativecommons.org/licenses/by-sa/4.0/)
![pipy](https://pypi.org/static/images/logo-large.72ad8bf1.svg)
The Python Package Index (PyPI) is a repository of software for the Python programming language.
You can install the python `pip` manager with the following command
**Some outdated distributions still have python2 as default python, use python3 command**
On Ubuntu
```bash
apt install python3-pip
```
On OSX
```bash
brew install python3
```
With docker:
```bash
docker run -it python:3.8-alpine sh
```
## installing packages with `pip`
And then you can install any packages you want on [https://pypi.org/](https://pypi.org/)
```bash
sudo pip3 install setuptools==50.0.2 # system wide install
sudo pip3 install setuptools==50.0.2 --user # user install
pip3 install twine wheel --user
```
`pip` is just another python module
```bash
python3 -m pip install --user --upgrade pip
```
## Packaging python projects
You can follow the official guide ([https://packaging.python.org/tutorials/packaging-projects/](https://packaging.python.org/tutorials/packaging-projects/))
### Creating the package files
This is the recommended basic structure your project should have to easily build a pip packages:
```
your_project/
├── LICENSE
├── README.md
├── example_pkg/
│ └── __init__.py
├── setup.py
└── tests/
```
But we can adapt it to follow the [LBMC guide of good practices](http://www.ens-lyon.fr/LBMC/intranet/services-communs/pole-bioinformatique/ressources/good_practice_LBMC)
```
your_project/
├── LICENSE
├── README.md
├── src/
│ └── example_pkg/
│ └── __init__.py
│ └── setup.py
│ └── tests/
```
All you python code goes in the `example_pkg/` folder. The most important file for the packaging is the `setup.py` file.
Here is a basic `setup.py` file:
```python
import setuptools
with open("../README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
version="0.0.1",
author="Example Author",
author_email="author@example.com",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
········"Programming·Language·::·Python·::·3",
········"License·::·OSI·Approved·::·CEA·CNRS·Inria·Logiciel·Libre·License,·\
version·2.1·(CeCILL-2.1)",
········"Operating·System·::·OS·Independent"
····],
python_requires='>=3.6',
)
```
- `name` is the distribution name of your package. This can be any name as long as only contains letters, numbers, _ , and -. It also must not already be taken on [pypi.org](https://pypi.org/). Be sure to update this with your username, as this ensures you won’t try to upload a package with the same name as one which already exists when you upload the package.
- `version` is the package version see PEP 440 for more details on versions.
- `author` and `author_email` are used to identify the author of the package.
- `description` is a short, one-sentence summary of the package.
- `long_description` is a detailed description of the package. This is shown on the package detail package on the Python Package Index. In this case, the long description is loaded from `README.md` which is a common pattern.
- `long_description_content_type` tells the index what type of markup is used for the long description. In this case, it’s Markdown.
- `url` is the URL for the homepage of the project. For many projects, this will just be a link to GitHub, GitLab, Bitbucket, or similar code hosting service.
- `packages` is a list of all Python import packages that should be included in the Distribution Package. Instead of listing each package manually, we can use find_packages() to automatically discover all packages and subpackages. In this case, the list of packages will be example_pkg as that’s the only package present.
- `classifiers` gives the index and pip some additional metadata about your package. In this case, the package is only compatible with Python 3, is licensed under the MIT license, and is OS-independent. You should always include at least which version(s) of Python your package works on, which license your package is available under, and which operating systems your package will work on. For a complete list of classifiers, see [https://pypi.org/classifiers/](https://pypi.org/classifiers/).
## Creating distribution archives
Now you just have to run the following command in the same directory as the `setup.py` file:
```bash
python3 setup.py sdist bdist_wheel
```
It will create files in the `dist/` directory
```
dist/
example_pkg_YOUR_USERNAME_HERE-0.0.1-py3-none-any.whl
example_pkg_YOUR_USERNAME_HERE-0.0.1.tar.gz
```
The `tar.gz` file is a Source Archive whereas the `.whl` file is a Built Distribution. Newer pip versions preferentially install built distributions, but will fall back to source archives if needed.
**You should always upload a source archive and provide built archives for the platforms your project is compatible with.**
img/cc_by_sa.png

1.48 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment