python import class from different folder - python

my project has folders that are structured like this:
main
-folder 1
-file1.py
-folder 2
-file2.py
both file1 and file2 having classes.
when I try from main.folder1.file1 import class1 it fails, saying "No module named main". What am I doing wrong and how should I import it?

you have to first create the module by including __init__.py
in the root directory which is same in the hierarchy of main folder and also create an __init__.py in other sub folders to make them accessible as modules.
Here is an example structure from the official documentation.Note how at each level there is __init__.py you have to include similarly.
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py
moduleA.py
your structure can be like below:
main/
__init__.py
folder1/
__init__.py
file1.py
folder2/
__init__.py
file2.py
Then you can append the path to the module at the top level like below.
You can have it in the __init__.py in the root directory.
sys.path.append(path.dirname(path.dirname(path.abspath(file))))
Then try accessing like from folder1.file1 import class1.
This should resolve your problem.
To further understand your problem read about modules and how to include relative imports by referring documentation.

In python you have to declare each folder as a module by adding a file named __init__.py in each directory including the root. This file can be empty.

you can do this if there are one level above.
import sys
sys.path.insert(0,'../folder1')
sys.path.insert(0,'../folder2')
import function1
import function2
....
Once you run the first 3 lines, you are adding those folders to the working directory. You can then import the functions within those files as if they are in the file already.
If the folders are in the same level, do this,
import sys
sys.path.insert(0,'folder1')
sys.path.insert(0,'folder2')
import function1
import function2
....

Related

How to access the module of a sub package

parent/
__init__.py
one/
__init__.py
module1.py
two/
__init__.py
three/
__init__.py
From this structure, I want to access module1.py
Accessing parent.one.module1 didn't work
You should be able to access module1 by importing the following:
import parent.one.module1
from parent.one import module1
From here, you should be able to reference the assets in module1
Let me know if this helps

Correct to import module class python

Python project structure:
src/
- package-name/
-- A/
---B/
b1.py
---C/
c1.py
In c1.py, it uses a function defined in b1.py. I try 2 ways:
Method1: from src.package-name.A.B.b1 import b1_func
Method2: from ..B.b1 import b1_func
The import module starts from package-name directory, so src/ will not be visible in the imported module. So Method1 not working when import my own module.
Method2 is not working when run in IDE. ValueError: attempted relative import beyond top-level package
Any suggestions? thanks.
Do you have __init__.py files in A and B? It may be worthwhile to properly import b1_func into B's and then A's init files.
B __init__.py
from .b1_file import b1_func
or whatever
and A __init__.py
from B import b1_func
Then you should be able to import ..b1_func
I change the "Content Root" to the package-name directory in PyCharm and import package-name.B.b1. It works.
follow these steps to import the packages wherever u want
First of all, add __init__.py in all folders
i.e: __init__.py in src and __init__.py in package and __init__.py in A
and __init__.py in B and __init__.py in C.
If u want to import the functions from b1.py in c1.py add these lines in c1.py file.
import sys
sys.path.append(“../”)
#if u want from src folder add ../../
from B.b1 import YourFunctionName

Importing from outside the directory

I have my project structure like this:
/application
/DIR1
__init__.py
file_that_import.py
file2.py
/DIR2
__init__.py
file_tobe_import.py
__init__.py
How should I import the file that is inside DIR2 from outside the directory?
You'll want to add the paths to the import path list like so
import sys
sys.path.append("/your/path")
Then import from those directories

Importing module with custom lookup

I have some problems with my project structure.
Because of historical reasons project looks like this:
Source/
__init__.py
module1/
__init__.py
script1.py
module2/
__init__.py
script2.py
and in production it deploys like this
server/
__init__.py
module1/
__init__.py
script1.py
module2/
__init__.py
script2.py
The problem is that script2.py has such imports:
from server.module1.script1 import something
Is it possible to say python to search server.*** not in server/*** but in Source/***?
Right now I made this with symlinks, but it looks ugly
You can always add keys to the sys.modules dictionary; these act as aliases for the module:
import sys
try:
import server
except ImportError:
import Source
sys.modules['server'] = Source
Once server is an entry in sys.modules, any sub-modules and packages will be found as well as the server entry will be used as a starting point for further imports.

python import within directory

I have a python project, I divided it into two parts, a library part and a
binary part.
For organization reasons, I wanted to have a hierarchy like this:
project/
lib/
__init__.py
module1.py
module2.py
bin/
binary1.py # contains: import module1
binary2.py # contains: import module2
doc/
...
The problem is, of course, the simple import in binary{1,2}.py doesn't work, at
least in the coding phase (before installation).
Any suggestions? How do you test your code when you're coding?
I use absolute imports everywhere.
project/
__init__.py
lib/
__init__.py
module1.py
module2.py
bin/
__init__.py
binary1.py # contains: import module1
binary2.py # contains: import module2
doc/
...
Setting PYTHONPATH to the directory above project, you can import module1 from binary1.py like this:
from project.lib import module1
The __init__.py files can make importing simpler, let's say in each file you have a class. Usually you would have to import the class like this (inclduding filename):
from project.lib.module1 import Module1
But if you edit lib/__init__.py to contain this line:
from project.lib.module1 import Module1
You can use the namespace of lib to import Module1 directly from lib:
from project.lib import Module1

Categories