Here are five different ways of how to accomplish that task.
For the following considerations I refer to Python 3.5+.
Register a custom Finder
Python uses finders for when importing modules. If a finder knows how to deal with a particular requested module then it returns a corresponding module spec and otherwise None
. Python has three different finders already registered which can be found in sys.meta_path
:
>>> import sys
>>> sys.meta_path
[<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]
The first one handles built-in modules, the second one frozen modules (some kind of "self-contained" Python scripts, see the wiki) and the last one handles everything which can be found on sys.path
. So if we modified sys.path
by appending '/home/project'
then it would be this finder which provides the corresponding spec.
Instead of modifying sys.path
we can register our own finder which uses the functionality of PathFinder
:
import importlib.machinery
class CustomFinder(importlib.machinery.PathFinder):
_path = ['/home/project']
@classmethod
def find_spec(cls, fullname, path=None, target=None):
return super().find_spec(fullname, cls._path, target)
Here we explicitly tell the PathFinder
to look into the /home/project
when importing modules.
We can register the finder as follows:
import sys
sys.meta_path.append(CustomFinder)
Then we can import the package aaa
which will be found by the CustomFinder
:
import aaa
For more information see PEP-302.
Extend sys.path
We can modify sys.path
in order to put the required package on the path:
import sys
sys.path.append('/home/project')
import aaa
from aaa import whatever
# Optionally remove the added path.
sys.path.pop()
Appending this directory to the path won't block "existing" (e.g. built-in packages) with the same name due to the order of searching that is performed during an import.
Add a local module containing a __path__
You can add a local module aaa.py
(in fact you can add it to any location which is on the Python path) which contains the following code:
__path__ = ['/home/project/aaa']
Then you can perform import
statements which will refer to the package that you referred to with the __path__
variable:
from aaa import whatever
If you want to import aaa
you can mimic this by applying the same method one level up in the directory hierarchy. Add a local module project.py
(for example) with the following code:
__path__ = ['/home/project']
Then you can do
from project import aaa
which is very much similar import aaa
if aaa
was on the path (provided that no other module named project
has precedence on the path).
Create a symlink pointing to the package
You can create a symlink that points to the package's directory. For example on Unix:
ln -s /home/project/aaa aaa
Then you can import the package via import aaa
, given you're executing this in the directory where you placed the symlink.
The symlink can also be created within your program via
import os
package = '/home/project/aaa'
target = os.path.split(package)[-1] # For example.
if not os.path.exists(target):
# `target_is_directory=True` is needed for Windows platform.
os.symlink(package, target, target_is_directory=True)
# Now import the package.
aaa = __import__(target)
Install the package via setuptools
You can add a /home/project/setup.py
script which contains (for example) the following code:
from setuptools import setup
setup(
name='aaa',
packages=[
'aaa',
# Add any sub-packages that `aaa` contains here.
]
)
Then you can install the package via cd /home && pip install -e project
and you can readily import it in your other Python files:
import aaa
from aaa import whatever
By using virtualenv
you can keep your installed packages in a clean state.