To create a Python package that can be installed with pip using the latest methods, while allowing you to modify the package without reinstallation or rebuilding, you can utilize the modern Python packaging standards defined in PEP 517 and PEP 660. This approach uses the pyproject.toml file for configuration and leverages editable installs that are compatible with the new standards.
Here's a step-by-step guide to set up your package:
1. Set Up Your Project Structure
Create a directory for your project and organize it using the modern "src" layout, which helps avoid certain import issues and is considered a best practice.
my_package/
├── pyproject.toml
├── src/
│ └── my_package/
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
my_package/: Root directory of your project.
pyproject.toml: Configuration file for your package.
src/: Contains your package source code.
my_package/: The actual Python package directory.
__init__.py: Makes Python treat directories as containing packages.
2. Write Your Package Code
Place your Python modules inside src/my_package/. Make sure to include an __init__.py file to indicate that this directory is a Python package.
Example __init__.py:
# src/my_package/__init__.py
__version__ = "0.1.0"
from .module1 import *
from .module2 import *
3. Create pyproject.toml
The pyproject.toml file is the modern way to configure Python packages. It specifies the build system requirements and metadata about your project.
Create a pyproject.toml file in the root of your project with the following content:
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my_package"
version = "0.1.0"
description = "A description of my local package"
authors = [
{ name = "Your Name", email = "you@example.com" }
]
# Include any package dependencies here
dependencies = [
# "dependency_one>=1.0",
# "dependency_two>=2.0",
]
[project.urls]
"Homepage" = "https://example.com/my_package"
[tool.setuptools]
package-dir = {"" = "src"}
packages = ["my_package"]
# If you want to automatically find packages, use:
# packages = { find = {} }
# Include other setuptools configurations as needed
Notes:
[build-system]: Specifies the build system requirements and backend.
setuptools>=61.0 is required for PEP 660 support (editable installs).
[project]: Contains metadata about your project.
[tool.setuptools]: Setuptools-specific configuration.
- We specify that our packages are in the
src directory.
4. Install the Package in Editable Mode
To install your package in a way that reflects changes immediately (an editable install), use the -e or --editable flag with pip:
pip install -e path/to/my_package
Or, if you're in the root directory of your project:
pip install -e .
Requirements:
pip>=21.3: Ensure your pip version is at least 21.3, which supports PEP 660.
setuptools>=61.0: Ensure your setuptools version supports editable installs under PEP 660.
You can upgrade them using:
pip install --upgrade pip setuptools
5. Modify Your Package Without Reinstallation
With the package installed in editable mode, any changes you make to the code in src/my_package/ will be immediately available when you import and use your package. There is no need to reinstall or rebuild the package after making changes.
6. Verify That Your Package Works
You can test that your package is correctly installed and that changes are reflected immediately:
# In a Python shell or script
import my_package
print(my_package.__version__)
# Make changes to the source code and test again
Additional Tips
- Dependencies: If your package depends on other packages, list them under
dependencies in pyproject.toml.
- Entry Points and Scripts: If you have console scripts or entry points, you can specify them under
[project.scripts] or [tool.setuptools.entry-points].
- Version Control: Consider using a version control system like Git to manage changes to your package.
- Virtual Environments: Use a virtual environment to isolate your package and its dependencies.
References
By following these steps, you'll have a Python package that can be installed with pip using the latest packaging standards. The editable install ensures that you can modify your package code, and the changes will be picked up immediately without the need for reinstallation or rebuilding.