- Modules
- dir() built-in function
- Packages - folders must contain
__init__.py
- Import package module -
import TopPackage.SubPackage1.module1
- Import package module -
from TopPackage.SubPackage1 import module1
- Import package module function -
from TopPackage.SubPackage1.module1 import my_func
- Package import searching
- pyenv to manage python language versions
- venv package management using pip-tools package versioning
- Creating PyPI packages
- Import package module -
- A module is a file containing Python statements and definitions
- Note that when importing a module, the interpreter executes the statements in the modules. This is a bit like the shell
source
statement
# import standard module math
import math
print("The value of pi is", math.pi)
print("The value of tau is", math.tau)
# import only pi from math module
from math import pi
print("The value of pi is", pi)
# import all names from the standard module math
from math import *
print("The value of pi is", pi)
print("The value of tau is", tau)
# import module by renaming it
import math as m
print("The value of pi is", m.pi)
- Interpreter first looks for a built-in module, if not found then into a list of directories defined in
sys.path
. The search is in this order;- The current directory.
- PYTHONPATH (an environment variable with a list of directory).
- The installation-dependent default directory.
- Note that if a module is imported using the
import
statement more than once, it is not re-executed, henceimp.reload(my_module)
-
__init__.py
must exist in each packare's folder. -
__init__.py
(and some Built-in functions) be empty or contain initialisation code for the package -
Organised hierarchically into sub-packages. E.g.
./TopPackage ./TopPackage/__init__.py ./TopPackage/SubPackage1/__init__.py ./TopPackage/SubPackage1/module1.py ./TopPackage/SubPackage2/__init__.py ./TopPackage/SubPackage2/module2.py
-
To call function
my_func
inmodule1
module;TopPackage.SubPackage1.module1.my_func()
-
To call function
my_func
inmodule1
module;module1.my_func()
-
WARNING:" This method is not recommended. Using the full namespace avoids confusion and prevents two same identifier names from colliding.
-
To call function
my_func
inmodule1
module;my_func()
- While importing packages, Python looks in the list of directories defined in sys.path
-
NB: On windows use pyenv-win
-
If not installed, run;
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
- Then add the following to bash profile and start a new shell;
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
- Find available verions;
pyenv install --list
- Install wanted versions;
pyenv install 3.11.0
pyenv install 3.7.15
- Set the default version to be used when no local version has been set;
pyenv global 3.11.0
- NB: This sets the version in
~/.pyenv/version
- cd into a directory (e.g. a repo root) and run;
cd <repo root dir>
pyenv local 3.7.15
- This creates a
.python-version
file in the current directory. Whenever in this directory or it's subdirectories, the local version will apply
- (assumes the virtual environment has been created in
.venv
dir - see below)
cd <some development dir>
source .venv/bin/activate
python3
- (assumes package versions are being managed in
requirements.txt
- see below)
cd <some development dir>
python3 -m venv .venv
source .venv/bin/activate
pip install pip==22.1 setuptools==60.10.0 wheel==0.37.1 pip-tools==6.6.1
pip-sync requirements_dev.txt
echo "/.venv" >> .gitignore
-
To add a new package, first edit
requirements.in
-
To start versioning a new package AND/OR refresh all existing packages in
requirements.txt
, run;cd <some development dir> source .venv/bin/activate pip-compile requirements.in pip-compile requirements_dev.in
-
To only start versioning new package or update only a specific existing package, run;
cd <some development dir> source .venv/bin/activate pip-compile requirements.in -P <some new package OR some existing package>
touch requirements.in
cat <<- EOF >> requirements_dev.in
-c requirements.txt
black
build
mypy
pylint
pytest
pytest-cov
EOF
cat <<-EOF >> .gitignore
__pycache__
/.mypy_cache
EOF
- Working example of this here; GitHub actions CI/CD and PyPI Python packaging