An example of python package

Overview

An example of python package

Why use packages?

It is a good practice to not code the same function twice, and to reuse common code from one python script to the other.

To import the function some_function defined in some_file.py, you can do:

from some_file import some_function

but this only works if you run your code in the same folder as some_file.py. If you want to make some_function accessible from anywhere on your computer, you should not use

import sys
sys.path.append("the/path/to/some_file.py")

because when you share your code with other people, this breaks most of the time.

Instead, you should create a python package containing the code you need. The following shows how to do it.

Package structure

This repository contains a basic python package, named my_package. Its structure is as follows:

example_package   (the main directory/folder)
├── my_package  (code folder, must have the name of the module)
│   └── __init__.py   (a special Python file, is executed whenever you import my_package)
|   └── utils.py   (a regular Python file in which you define functions, variables, classes, etc)
├── setup.py  (this special file must be executed to install the package)
└── README.md   (contains what you are currently reading)

The mandatory files, which must have exactly this name, are setup.py and my_package/__init__.py. On the other hand, note that:

  • utils.py could have an arbitrary other name
  • README.md is not necessary for a package, and is used here to give information to the people browsing the Github repository

Package installation

Once you have the files defined above, you should open a terminal, move to where the setup.py file is (using the cd command), then execute pip install -e .

After that, from any location on your computer you can open an ipython terminal and run:

import my_package
from my_package import my_function
my_function()
# etc, just like when you do: from pandas import read_csv

How does it work?

Running pip install -e . tells python to remember where it should look when you refer to my_pakage in some code. Whenever you run import my_package, it will go to this location, and run the __init__.py. Inside the __init__.py, you have imported or defined some variables (functions, classes, constants, etc), that are now usable in your main script.

More advanced

Submodules

When you do:

from sklearn.linear_model import Lasso

you are using the submodule linear_model of sklearn. When you codebase grows, splitting it into submodules is nice to keep your code organized (for example, all code related to Linear Models go into the linear_model submodule; preprocessing go into sklearn.preprocessing, etc).

in simple terms, a submodule is a package defined inside a package (meaning it also has its own `init.py), using this folder structure:

example_package
├── my_package
    └── __init__.py
    └── my_submodule
        └── __init__.py

usually, the __init__.py file import variables defined in other files inside the my_submodule folder (not shown here for simplicity).

You might also like...
BOHB tune library template (included example)
BOHB tune library template (included example)

BOHB-template 실행 방법 python main.py 2021-10-10 기준 tf keras 버전 (tunecallback 방식) 완료 tf gradienttape 버전 (train_iteration 방식) 완료 pytorch 버전은 구현 준비중 방법 소개

Assembly example for CadQuery
Assembly example for CadQuery

Spindle and vacuum attachment This is a model of the vacuum attachment for my Workbee CNC router. There is a mist spray coming from the left hand side

An example file showing a simple endpoints like a login/logout function and maybe some others.

Flask API Example An example project showing a simple endpoints like a login/logout function and maybe some others. How to use: Open up your IDE (or u

Script to produce `.tex` files of example GAP sessions

Introduction The main file GapToTex.py in this directory is used to produce .tex files of example GAP sessions. Instructions Run python GapToTex.py [G

An example project that shows how to check if a certain macro is active in a file.

PlatformIO Check Compiler Flags Example Description Demonstrates the usage of an extra script and a special compilter invocation to get the active mac

Architecture example simulator

SCADA architecture Example of a SCADA-like console application, used to serve as a minimal example of a standard architecture of an IIoT system. Insta

The ROS publisher/subscriber example packaged as a snap

publisher-subscriber The ROS publisher/subscriber example packaged as a snap, based on ROS Noetic and Ubuntu Core 20. Strictly confined. This example

An example repository for how to generate results using PyBaMM

PyBaMM results This repository provides a template for generating results (for example, for a paper) using PyBaMM Installation Install PyBaMM using a

A tool to nowcast quarterly data with monthly indicators: US consumption example

MIDAS_Nowcaster A tool to nowcast quarterly data with monthly indicators: US consumption example Pulls data directly from FRED from a list of codes -

Owner
null
This is an example manipulation package of for a robot manipulator based on Drake with ROS2.

This is an example manipulation package of for a robot manipulator based on Drake with ROS2.

Sotaro Katayama 1 Oct 21, 2021
A test repository to build a python package and publish the package to Artifact Registry using GCB

A test repository to build a python package and publish the package to Artifact Registry using GCB. Then have the package be a dependency in a GCF function.

null 1 Feb 9, 2022
Python Example Project Structure

Python Example Project Structure Example of statuses that can be in readme: Visit my docs for the full documentation, examples and guides. With this p

null 1 Oct 31, 2021
An example of Connecting a MySQL Database with Python Code

An example of Connecting a MySQL Database with Python Code And How to install Table of contents General info Technologies Setup General info In this p

Mohammad Hosseinzadeh 1 Nov 23, 2021
An example of Connecting a MySQL Database with Python Code

An example of Connecting And Query Data a MySQL Database with Python Code And How to install Table of contents General info Technologies Setup General

Mohammad Hosseinzadeh 1 Nov 23, 2021
Buffer overflow example for python

Buffer overflow example for python

Mehmet 1 Jan 4, 2022
That is a example of a Book app on Python, made with support of all JS libraries on React framework

React+Python Books App You can use this repository whenever you want Used for a video Create the database: python -m dbutils Start the web server: pyt

Koma Human 1 Apr 20, 2022
This is a survey of python's async concurrency features by example.

Survey of Python's Async Features This is a survey of python's async concurrency features by example. The purpose of this survey is to demonstrate tha

Tyler Lovely 4 Feb 10, 2022
Packages of Example Data for The Effect

causaldata This repository will contain R, Stata, and Python packages, all called causaldata, which contain data sets that can be used to implement th

null 103 Dec 24, 2022
An example using debezium and mysql with docker-compose

debezium-mysql An example using debezium and mysql with docker-compose The docker compose starts the Zookeeper, Kafka, Mysql and Debezium Connect. Aft

Horácio Dias Baptista Neto 4 May 21, 2022