this.guy
this.guy

Reputation: 91

Import custom module from other directory

Let's say I have a custom module in:

/basexx_yy/subdir1/subdir2/subdir3/subdir4/custom_module.py

And the script which needs to import custom_module.pyis located at:

/basexx_yy/subdir1/otherdir/script.py

The basexx_yy is a dir with a dynamic name, consider xx and yy to be time stamps, let's say for the sake of clarity that xx is day of the week (01 = Monday - 05 = Friday) and yy is week number (subdir1 - subdir4 are constant). So the full path to custom_module.py cannot be included as a static address. Since the subdirs are constant, I wrote the following code:

import os
import sys
cwd = os.getcwd()
split = cwd.split('\\')
if 'subdir1' in split:
    parentdir = cwd.split('subdir1')
    sys.path.insert(0, os.path.join(parentdir[0], 'subdir1', 'subdir2', 'subdir3', 'subdir4'))
else:
    sys.exit("'subdir' dir not found! Run the script from within basedir.")

import custom_module

It does not, however, work. I'd appreciate some clarity as I cannot see why this doesn't work.

Upvotes: 0

Views: 253

Answers (2)

Nils Werner
Nils Werner

Reputation: 36749

It is not exactly clear from your question what your situation is, but here goes as it was designed to be used:

  • A package contains a setup.py and can be installed using pip.
  • A module lives inside a package or a module and contains an __init__.py.

Your package should have a structure like

setup.py
basexx_yy/
          __init__.py
          something_else.py
          subdir1/
                  __init__.py
                  more_files.py
                  otherdir/
                           script.py
                  subdir2/
                          __init__.py
                          etc.py
                          subdir3/
                                  __init__.py
                                  pp.py
                                  subdir4/
                                          __init__.py
                                          custom_module.py

basexx_yy being a package means it can be installed into your python library collection using

pip install basexx_yy

or, while developing

pip install -e basexx_yy

Afterwards any script (it may even be completely outside your package) can do

import basexx_yy

and also deep import like

import basexx_yy.subdir1.subdir2.subdir3.subdir4.custom_module as cm

cm.sqrt(4)

or, any file in your module tree (otherdir is missing __init__.py so it is outside the module tree and cannot do this) can do relative imports.

e.g. custom_module.py can do

from . import custom_modules
from .. import pp
from ... import etc
from .... import more_files
from ..... import something_else

Upvotes: 2

luv
luv

Reputation: 432

I agree with Nils Werner, that you most probably want to restructure your package(s).

Anyway, your code should work! Only problem is that you use

split = cwd.split('\\')

instead of

split = cwd.split(os.path.sep)

Upvotes: 0

Related Questions