Importing from outside the directory - python

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

Related

python import class from different folder

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
....

Importing from folders at the same hierarchy level

I've been looking around this site for how I can import a class stored in a folder at the same hierarchy level. What I found is that using .. should bring me up one folder. Or at least, that is how I read it as that assumption seems to be wrong.
src/
folderStrucutre1/
__init__.py
fileToImport.py <- contains A
folderStrucutre2/
someFile.py
__init__.py
abc.py
Having above folder structure in which fileToImport.py contains a class named A. How would I import A into someFile.py?
Due to how packages work in python, you need to move src and abc.py into a subfolder, and provide an __init__.py for it.
The directory structure should look like this after the changes:
package-name/
package-name/
folderStructure1/
__init__.py
fileToImport.py <- contains A
folderStructure2/
__init__.py
someFile.py
__init__.py
abc.py
Then, in someFile.py you can import A using a relative import from the parent package:
from ..folderStructure1.fileToImport import A
Lastly, you should open the topmost folder (parent to abc.py) for IDE intellisense to work
First, we need to create the absolute path to your src folder without hard-coding it in your script to make it portable.
Add this code in your someFile.py file.
import os
src_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Now, let's add your folderStructure1 directory to the Python Search Path
import sys
sys.path.append(src_path + '/folderStructure1')
Now you can use the following:
from fileToImport import Class
object = Class()
You can add src to the python search path and then you can import A from fileToImport.py.
For this, you should write someFile.py like:
import sys
sys.path.append("..") # .. represente the father folder
from folderStrucutre1.fileToImport import A
instance_for_A = A()

how to make relativ or absolute imports work

i have a file path that looks like this:
project_folder
|--sub_folder
| |--file1.py
|--file2.py
now in file1.py i would like to import file2.py but my attempts don`t work:
#this is what i tryed in file1.py:
from . import file2.py
from .. import file2.py
from ...project_folder import file2.py
from project_folder import file2.py
import ..file2.py
import project_folder.file2.py
everything is failing with: "ImportError: attempted relative import with no known parent package".
I`ve also tried to add a __init__.py file to the project_folder and the sub_folder but that did not help.
I`m not shure if anything has to be in these __init__.py files or if they can stay empty.
for me the answer is: Just import the files that are inside and not outside a folder. If you import a file that is outside the folder it can break you Programm because you created an import loop.
So if you think you have to do a relative import you might have to rethink your file system.

Python3 import modules from folder to another folder

my structure dictionary is
mainFolder
folder1
__init__.py
file1.py
file2.py
folder2
__init__.py
file3.py
file4.py
setup.py
__init__.py
i need import file4.py from folder2 to folder1/file1.py
file1.py:
from ..folder2.file4 import MyClass
and i gets:
SystemError: Parent module '' not loaded, cannot perform relative
import
how to fix that ?
This is because you must to explicitly name the parent package.
So in your case you need either from mainFolder.folder2.file4 import Myclass, either from folder2.file4 import Myclass

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