RajeshS
RajeshS

Reputation: 69

Setting Env Python Path at run time


I have my code in the below structure

Folder Libs:
init.py

import lib1
import lib2
import lib3

Folder Tests

import sys
import os
sys.path.append(os.path.abspath('../Libs'))

import lib1

I get the error lib1 not found, any idea what could be wrong? the sys.path.append should have included the folder Libs to the path and so the library lib1, lib2 & lib3 should have ideally be included. Not sure why its not recognized. Any suggestion please?

Upvotes: 0

Views: 74

Answers (1)

tlentali
tlentali

Reputation: 3455

You may try the following knowing that ClassNameLib1 and ClassNameLib2 are the names of the classes in the lib1_file and lib2_file files containing those classes placed in the lib1 and lib2 folders :

import sys

sys.path.append("..")

from libs.lib1.lib1_file import ClassNameLib1
from libs.lib2.lib2_file import ClassNameLib2

However, we can also add some informations in the __init__.py in the folder libs using the __all__ variable :

__all__ = [
    'lib1',
    'lib2',
    'lib3'
]

And in the folder lib1 we can add also an __init__.py file containing for all folders and files :

from .lib1_file import ClassNameLib1

__all__ = [
    'ClassNameLib1'
]

Doing that, it allows you to only call import libs to get everything you need in your project.

Upvotes: 1

Related Questions