Compiled on: 2024-10-19 — printable version
import
keyword.from
keyword.as
keyword.# You can import a module
import math as m
from math import sqrt
# You can use the imported module
x = m.sqrt(25) # 5
y = sqrt(36) # 6
math
is a built-in module in Python.__init__.py
.import
keyword.from
keyword.# You can import a package
import numpy
# You can import a module from a package
from numpy import random
# You can use the imported package
y = random.randint(1, 10) # random integer between 1 and 10
numpy
is a third-party package in Python!In the package example we used numpy
, but because it is a third-party package, we need to install it first.
In general, there could be many dependencies in a project, and it is hard to manage them manually.
The need for build systems in Python emerged with more complex use cases.
Anaconda | Conda | Miniconda | pip |
Poetry | PyBuilder | PyEnv | virtualenv |
Since there were no standard management systems originally, multiple tools proliferated
By default, Python is installed system-wide
All Python installations come with pip
, the package installer for Python
So, one may install Python packages system-wide with pip install PACKAGE_NAME
One problem, many implications: the same package can be installed only once on the same Python installation
- (a) what if two projects on the same system require different versions of the same package as dependencies?
- say, project A requires
Kivy==2.3
and project B requiresKivy==1.4
- (b) what if two projects on the same system require different versions of Python?
- say, project A requires Python
3.8
and project B requires Python3.10
(consider reading this page for further details https://stackoverflow.com/a/41573588)
virtualenv
and venv
are tools to create virtual Python installations on the same system
virtualenv
is a third-party tool, venv
is built-in in Python 3.3 and laterv. XXX
installed on your system…
v. XXX
in other folders
pip
PyEnv
is a tool to manage multiple Python installations on the same system
New problem: many Python installations on the same system,
each one with a different version of Python, and different packages installed
recall all the issues we had in previous lectures?
Smart and adequate convention to work with Python projects: 1-project-1-Python-env
Achieving this requires developers to be disciplined and meticulous
Poetry
is a tool that aims to automate this process
Poetry is a declarative tool for dependency management, packaging, and release in Python
It handles both dependencies and dev-dependencies
requirements.txt
and requirements-dev.txt
It automates the 1-project-1-Python-env convention
It simplifies the packaging process for the project
It simplifies the publication process on PyPI (or other software repositories)
Poetry
is a modern dependency management tool for Python.Poetry
with the following command:curl -sSL https://install.python-poetry.org | python3 -
# Create a new project
poetry new my_project
# Add a dependency
poetry add numpy
# Install dependencies
poetry install
# Run a script
poetry run python my_script.py
<root directory> # main directory of the project
├── <package>/ # main package of the project (there can be multiple packages)
│ ├── __init__.py # python package marker
│ └── <module>.py # module in the package (there can be multiple modules)
├── test/
│ └── test_<module>.py # test module for the module
├── .github/
│ └── workflows/
│ └── check.yml # some github action
├──.gitignore # git ignore file
├──.gitattributes # git attributes file (e.g., for line endings in different OS)
├──__main__.py # application entry point
├── LICENSE # license file (e.g., Apache 2.0)
├── pyproject.toml # build dependencies
├── poetry.toml # Poetry settings (we will see Poetry for dependency management)
└── README.md # don't forget to write a README file