Useful scripts with common usage across multiple projects.
Clone the repository
git clone https://github.com/wuvin/wutilsCreate environment to install package (optional)
python -m venv .venv
source .venv/bin/activate # if on Ubuntu; on Windows, .venv\Scripts\activateInstall package and dependencies
(Note: setup.py is configured to read requirements.txt)
pip install .A way to modularize code development is to turn a collection of similar Python scripts into a package from which another project can import functions. Below is a simple guide.
The root directory should follow a standard structure:
wutils/
├── __init__.py
├── example_module_my_functions.py
└── example_module_other_functions.py
setup.py
requirements.txt
README.md
LICENSE
wutils/: This is the package directory, which is distinct from the root directory. The name of the root directory is not so important -- it can be the same as the package -- but the name of the package directory must be the name used to import the package.__init__.py: This is a required file that indicateswutilsto be treated as a package. It can be empty, but it has other optional uses:- Initialize package-level data.
- Define what gets imported by
from wutils import *. - Make certain functions or sub-modules directly accessible at package level.
example_module_my_functions.pyandexample_module_other_functions.py: These are the modules belonging to the package. Each script containing functions becomes a module.setup.py: Dependency installation is managed by a specially-named executable or setup file such as this. The modern standard ispyproject.toml, which is another option, but it is fine to usesetup.py.requirements.txt: This is the blueprint file thatpipuses to install project dependencies within an environment. There are alternatives;condaenvironments can useenvironment.ymlorenvironment.yaml.
Technically optional, but doing so is standard for managing dependencies when sharing development. This involves adding a setup.py file or similar. With setup.py, another user can install the package in a new virtual environment by executing pip install . within the root directory.
During development, if you want any changes to the source files in wutils to be immediately reflected in the installed package without needing to reinstall, then you can install in editable mode: pip install -e ..
Within an existing environment, all dependencies can be exported by executing:
pip freeze > requirements.txtThis is simple and quick, but it does list all packages of the active environment, not just those specific to the project. A new installation may capture unnecessary dependencies.
# pyproject.toml
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "wutils"
version = "0.1.0"
description = "A collection of broadly useful Python functions"
authors = [
{name = "Your Name", email = "your.email@example.com"},
]
readme = "README.md"
requires-python = ">=3.8"
license = { file = "LICENSE" }
keywords = ["utilities", "math", "strings"]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: Ubuntu 22.04",
]
[project.urls]
Homepage = "https://github.com/wuvin/wutils"
Repository = "https://github.com/wuvin/wutils.git"Kevin Wu
wu.kevi@northeastern.edu
Inspiration, code snippets, etc.