Reputation: 1488
Say I have a directory with some .py files, and some directories with other .py files
That directory is called my_program
my-program
-- dir1
-- file.py
-- dir2
I want to be able to use my_program's defined modules from any directory, say by doing
>>> import my_program
>>> myprogram.dir1.file.function()
or by doing
>>> import my_program.dir1.file
>>> file.function()
I also want to "install" my module for systemwide acessibility using pip
Upvotes: 4
Views: 2005
Reputation: 1488
A minimal working example follows
my_packs/
├── foo
│ ├── code.py
│ └── __init__.py
└── setup.py
here code.py constains some useful function, and setup.py contains
from setuptools import find_packages, setup
setup(name="foo",
version="0.1",
description="A foo utility",
author="Ewen Cheslack-Postava",
author_email='[email protected]',
platforms=["any"], # or more specific, e.g. "win32", "cygwin", "osx"
license="BSD",
url="http://github.com/ewencp/foo",
packages=find_packages(),
)
and init is an empty file
the package can be installed with
python setup.py develop
or
pip install -e .
and then, from any dir, it runs like follows:
python -c 'import foo.code; print(foo.code.useful_function())'
Example adapted from: https://ewencp.org/blog/a-brief-introduction-to-packaging-python/index.html
Upvotes: 3