Skip to content

Latest commit

 

History

History
268 lines (190 loc) · 7.87 KB

Python_crib_notes-Modules_packages_and_import.md

File metadata and controls

268 lines (190 loc) · 7.87 KB

Modules, packages and import

< Back



Modules

  • 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 module name only - import module_filename.py

# import standard module math

import math
print("The value of pi is", math.pi)
print("The value of tau is", math.tau)

Import specific names - from module_filename import name1, name2, ...

# import only pi from math module

from math import pi
print("The value of pi is", pi)

Import the all names - from module_filename import *

# 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)

Rename module - import module_filename as

# import module by renaming it

import math as m
print("The value of pi is", m.pi)

Module import searching

  • 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.

Reload a module - imp.reload(my_module)

  • Note that if a module is imported using the import statement more than once, it is not re-executed, hence imp.reload(my_module)

dir() built-in function

Names defined in a module - dir(module_name)

Names in current namespace - dir()

Packages - folders must contain __init__.py

  • __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

Import package module - import TopPackage.SubPackage1.module1

  • To call function my_func in module1 module;

    TopPackage.SubPackage1.module1.my_func()

Import package module - from TopPackage.SubPackage1 import module1

  • To call function my_func in module1 module;

    module1.my_func()

Import package module function - from TopPackage.SubPackage1.module1 import 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 in module1 module;

    my_func()

Package import searching

  • While importing packages, Python looks in the list of directories defined in sys.path

pyenv to manage python language versions

Install pyenv

  • 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 -)"

Install specific python versions

  • Find available verions;
pyenv install --list
  • Install wanted versions;
pyenv install 3.11.0
pyenv install 3.7.15

Set global python version

  • 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

Set local python 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

venv package management using pip-tools package versioning

Use existing virtual environment

  • (assumes the virtual environment has been created in .venv dir - see below)
cd <some development dir>
source .venv/bin/activate
python3

Create .venv dir and install versionsed packages

  • (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

Manage package versions in requirements.txt

  • 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>

Basic requirements.in and requirements.dev

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

Creating PyPI packages