Search code examples
pythonpathsys

Add python package subdirectory to system path


I have a python package that has the following structure:

Package
├───ExternalModules
│   ├───external_module.exe
│   └───external_module_importer.py
└───setup.py

The package is installed with the setup.py file ("pip install ./") and in it, there is a library/dependency that relies on the executable_module.exe to run.

I don't want my users having to download the module and add the path to it manually, so I want to have it on my package and add it to the system path automatically (during package installation or on runtime).

Currently, I have this external_module_importer.py trying to import it in the following way:

import os
import sys

def add_module_to_path():
    filedir = os.path.dirname(os.path.abspath(__file__))
    sys.path.append(filedir)

but even if I call this function, the external dependency that relies on the .exe file gives me the following error:

FileNotFoundError: external_module not found or not executable, check the configuration file

If I manually add Package/ExternalModules/ folder to PATH, it works.

Is there a good solution for it?


Solution

  • Answering myself:

    When I did append to sys.path, I was adding the directory to the PYTHONPATH environment variable, not the system PATH variable.

    To do de former the necessary command is this:

    os.environ["PATH"] += os.pathsep + path
    

    This is system agnostic.