Python3 Importing module/package from sibling directories - python-3.x

Here is my code directory structure:
/root
-/proj1
--/module1.py
--/__init__.py
--/sub_proj1
---/module2.py
---/__init__.py
-/proj2
--/module3.py
If I want to import code from module2.py into module3.py I tried the following import statement:
from .proj1.sub_proj1 import *
but I am getting import error. Is thee anything I need to fix? I am not sure what I am doing wrong with relative imports.

Similar to this question:
Python import module from sibling folder
You need an __init__.py in your root and both project folders
Also this should be your import statement:
from ..proj1.sub_proj1 import *

Related

Cannot import functions in from one directory's subfolder to other pyhton files in same parent directories sub folders. How do I use init.py files

parent_folder /
subfolder1 /
subsubfolder1/
a.py
b.py
subsubfolder2/
c.py
d.py
e.py
subfolder2 /
subsubfolder2/
f.py
g.py
subfolder3 /
h.py
i.py
g.py
I want to import the functions from files h,i,g to all the subsubfolders py files. Can any one help me how do I do that.
I have tried using sys.path.insert(0 , 'path')
from h import funct1 , from g import funct1. It works
II get this error ImportError: attempted relative import with no known parent package when I do from .h import funct1 and I also want to Implement this in docker files. Is there any other way?
Thanks in advance !!
For your case it is best to use sys:-
for example to import in a.py use:
import sys
sys.path.insert(0,'../../subfolder3')
import h,i,g
Here we can use relative path in sys, '..' indicates moving one folder back
For Docker it is best to use relative path as:
import sys,os
sys.path.append(os.path.join(sys.path[0],'../../subfolder3'))
import h,i,g
This would work for docker. Please try and let me know if it works.

Relative imports in python and pycharm

I have following project structure and i'm using PyCharm.
Project/
subproject1/
main.py
modules/
1.py
__init__.py
subproject2/
main.py
modules/
subproject3/
__init__.py
file.py
I need in Project/subproject/modules/1.py a class from Project/subproject3/file.py. I tried it with from ...subproject3.file import class1, but then i get the error ValueError: attempted relative import beyond top-level package. Next i tried the import from subproject3.file import class1, and that work's, but i don't know why. My working directory in PyCharm is in Project/ but i tried to create a Python application with pyinstaller and that worked also for me and a received no error message. Why does my file 1.py knows from subproject3 and i can import it that way?
Thank you!!

Importing Module issue (name of project and name of module are the same)

So I am having an issue trying to import some modules right now because the name of the project is same as the module name. At least thats what I think.
I am trying to run the test_aws_file.py main file. That file is trying to import my.test.utils.util_file, but it is getting this error:
ModuleNotFoundError: No module named 'my.test.aws', so seems like i can import up to 'my.test' then when i add .aws then it would cause the error
test_aws_file.py import coding:
sys.path.insert(1, os.path.abspath('.'))
print(sys.path)
from my.test.aws.utils.utils_file import UtilsFile
project name (i do have a init.py in all directories): my.test.aws
my/
test/
aws/
utils/
util_file.py
test_aws/
test_aws_file.py
you should have __init__.py files in your directories to make those packages, otherwise the import wont work.

Attempted relative import beyond top-level package Error

I have a folder xyz which contains two files ab.py and cd.py. Im trying to import cd.py in ab.py file.
Below is the folder structure:
/xyz
ab.py
cd.py
I have to import cd to ab
when i do
from . import cd
This gives me error as:
Attempted relative import beyond top-level package
If I understand correctly. xyz is a directory under your working directory.
In that case try to add the current directory to import path.
>>> import sys
>>> from pathlib import Path
>>> sys.path.append(Path.cwd())
And in ab.py use from xyz import cd
Additional references
Sibling package imports
Relative imports for the billionth time

Importing submodules

I am new to python and i m having a really bad time to overcome a problem with the importing system.
Lets say i have the file system presented below:
/src
/src/main.py
/src/submodules/
/src/submodules/submodule.py
/src/submodules/subsubmodules
/src/submodules/subsubmodules/subsubmodule.py
All the folders (src, submodules, subsubmodules) have and empty __init__.py file.
In submodule.py i have:
from subsubmodules import subsubmodule
In main.py i have:
from submodules import submodule
When i run submodule.py python accepts the import. But when i run main.py python raises error for the import of subsubmodule.py because /src/submodules/subsubmodules/ folder is not in the path.
Only solution is to change the import of submodule.py to
from submodules.subsubmodules import subsubmodule
This seems to me as an awful solution because after that i cannot run submodule.py and i m sure that something else is the key to that.
An other solution is to add the following code to the __init__.py file:
import os
import sys
import inspect
cmd_subfolder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
Is there any way to do this using just the importing system of python and not other methods that do it manually using, for example sys.path or other modules like os, inspect etc..?
How can i import modules without caring about the modules they import?
You can run subsubmodule.py as
python3 -m submodule.subsubmodules.subsubmodule
If you want a shorter way to invoke it, you're free to add a shell or Python script for that on the top level of your package.
This is how imports work in Python 3; there are reasons for that.
You can avoid this issue by using sys.path in your program.
sys.path.insert(0, './lib')
import subsubmodule
For this code, you can put all your imports to a lib folder.
You can read the official documentation on Python packages where this is explained in depth.

Resources