[TOC]
- Title: Python Module and Package Management 2023
- url: https://www.youtube.com/watch?v=v6tALyc4C10
Python module
common issue 1: python -m dir
vs python dir
python dir
will use the dir as thePYTHONPATH
as run__main__.py
file.- however,
python -m dir
will not use the dir asPYTHONPATH
but rather it will search the local library.- thus
ModuleNotFoundError
may appear
- thus
Standard way of import when you want to create a module
- use absolute path ->
from moduledir/packagename import filename
- avoid using relative path
- e.g.,
from .xxx import yyy
- e.g.,
- if within the same module, you can just use
from xxx import yyy
However, in a complicated project where there are too many modules.
- one workaround is that we still make it a module, but try to append folder path to the system path.
|
|
- always use it as the last resort.
Solution: setup pyproject.toml
and install it as a proper module
-
python -m venv .venv
-
pip install -U pip
-
pip install -U setuptools
-
pip install -r requirements.txt
-
pyproject.toml
should be outside of the code dir -
https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html
Sample code
- hydra
default.yaml
config
|
|
pyproject.toml
|
|
Hydra module
|
|
- you can add this line for debugging
|
|
.vscode/settings.json
- https://code.visualstudio.com/docs/editor/variables-reference
- python.envFile is for interactive window (IPython)
|
|
- a sample structure can be
|
|
- once we setup the toml file, we can use
pip install -e .
Extra: where do these square brackets do in pip install
- we often see
|
|
- you are installing splinter package which has the added support for django, the django is the extra dependencies that require further installation.
- in pyproject.toml, we have the following config to control the extra dependencies
|
|
Extra: what is [tool.setuptools.packages.find]
Extra: useful tools that can integrate within pyproject.toml
|
|
Extra: update gitignore
- when we want to update gitignore, often we need to remove all the cache and then add it back again
|
|
Useful VSCode python extension
- Python Extension Package
- GitGraph
- Remote - SSH
- Jupyter
- file-size
- psioniq File Header
- Better Comments
- Choose a License
- latex workshop
- code spell checker
- black formatter