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?
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With