1.6 KiB
1.6 KiB
How to add a custom syspath for Python
When importing libraries, Python will check in the sys.path to know which
subdirectories might contain a package. It will mark any directory containing a
__init__.py
file as a package or subpackage if its parent also has one.
In order to create a package you then simply have to a have a structure like:
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/ Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...
And to be able to import this package from anywhere you can simply add the
absolute path to sound
to your syspath before importing it.
Method 1
import sys
import os
# if you maximum priority you can add it first
sys.path.insert(0, os.path.abspath('/path/to/your/package/'))
# of add it at the end if you does not want priority
sys.path.append(os.path.abspath('/path/to/your/package/'))
Method 2
You can use the PYTHONPATH
env variable to add new paths to your syspath like
so:
$ PYTHONPATH="/path/to/your/package/" python3