Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a library which is in a sibling of the current folder

With the folder structure

lib/
    abcd/
        __init.py__
        lib.py
app.py

the code

from lib.abcd import lib

works. But with this file structure:

bin/
    app.py
lib/
    abcd/
        __init.py__
        lib.py

the code

from ..lib.abcd import lib     

gives an import error.

How to do the import properly when the library is in a sibling of the current folder? (or subfolder of a sibling folder)

I know that there might some hack that involves adding lib/ to the PATH, but is there an elegant Pythonic solution?

If not, is there a real internal reason to prevent users to do this simple import in a simple way?

like image 899
Basj Avatar asked Feb 10 '19 23:02

Basj


People also ask

How to import files from the sibling directory in Python?

In this article, we will discuss ways to import files from the sibling directory in Python. First, create two folders in a root folder, and in each folder create a python file. Below is the dictionary tree: In B.py we will create a simple function and in A.py we will import the sibling module and call the function created in B.py.

How does the library importer work?

The Library Importer features the ability to export a detailed text-based file that captures the current configuration setup, which includes all configurable import settings such as target paths, parameter mapping, naming scheme, lifecycles definitions, target component types, and so on.

How do I use the library importer in simple mode?

The Library Importer is available when you are connected to a Workspace. To access the importer in its Simple mode: In the Components panel , choose the Import Library option from the menu.

How do I import specific items from a library or module?

Using the keywords from…import we can import specific items from a library or module. These may include functions or classes within that specific library. This is important especially when you do not intend to use only a specific function in a module and therefore it is needless to import the entire module.


1 Answers

Methods to do this


Method #1: Using the sys module: You can easily accomplish what you are trying to do using the sys module. To import the lib package, you can use one of the two codes listed below:

import sys
sys.path.append('<PATH_TO_LIB_FOLDER>')

from lib.abcd import lib

or

import sys
sys.path.insert(0, '<PATH_TO_LIB_FOLDER>')

Method #2: Using the os module: Another method is to use the os module. Here is an example code that imports the lib module using the os module by invoking the os.path.join method:

import os

path = os.path.join("<PATH>/lib/abcd", "lib")
from lib.abcd import lib

Method #3: Add the module to your PYTHONPATH: This is not the best method in most cases, but if you don't want to keep using the sys or os module to import lib, this is ideal. All you have to do is type this in your bash terminal:

export PYTHONPATH=<PATH_TO_LIB> python lib.py

Then in your python shell you can import it like this:

from lib.abcd import lib

Method #4: Combine the sys and os module (recommended): This is the most efficient method and will save you a lot of time. This code combines the os and sys module like this:

import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'lib')))

Then you can import your module with ease like this:

from lib.abcd import lib

How all the codes work:

All the codes above are very simple. All the examples except for "Method #3", add your module to the PYTHONPATH temporarily. "Method #3" on the other hand, adds the module to your PYTHONPATH permanently.

like image 183
xilpex Avatar answered Oct 17 '22 08:10

xilpex