Relative imports in python and pycharm - python-3.x

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

Related

Python - Unable to import '...' when __init__.py file is in directory

I have the following directory structure :
myproject
__init__.py
src
__init__.py
main.py
common
__init__.py
logging
__init__.py
setup.py
All init files are empty.
Why is it that in main.py I cannot do :
from src.common.logging import *
I get an error stating it is unable to import. I thought the __init__.py files here would have prevented this from happening.
I've tried from common.logging import * as well, but didn't work either.
Ideally I don't want to add anything to my path and I was expecting this to work with only __init__.py files in a clean/pythonic way.
Found my answer.
It was actually my linter complaining that the import was not possible, but this was due to a wrong python path specified in my editor. When executing the code, all imports worked fine.
I was using visual studio code.
Make sure you set your python path in settings.json
"python.pythonPath": "/__venv__/bin/python3"

Getting no-name-in-module on pylint when having duplicates names

My current folder structure is something like this:
folder/
utils.py
something.py
etc.py
In something.py I do some imports:
from folder.etc import x
import utils.some_module
utils is a package installed from pip that unfortunately has the same name as one of my files inside folder. The code works normally but for some reason pylint gives me this error:
folder/something.py:11: [import-error(pylint), None] Unable to import 'utils.some_module'
folder/something.py:11: [no-name-in-module(pylint), None] No name 'some_module' in module 'utils'
If I change the file name from utils.py to something else the error disappears but I don't want to change it. The structure of package utils is as follow:
utils/
__init__.py
some_module
Does anyone have any idea how to fix this problem without changing the file name?
Thanks

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.

How can I import a module from a parent directory?

I have the following directory structure for my Python project:
lib
__init__.py
tasks.py
api(directory)
__init__.py
app.py
Now, I want to import the tasks module into app.py. If I just type
import tasks
it magically works in Pycharm, but when executed via bash I get an error message saying "No module named tasks". I also tried
from .. import tasks
which gives me
ValueError: attempted relative import beyond top-level package
What am I doing wrong? Why does import tasks work when executed in Pycharm?

Python3 Importing module/package from sibling directories

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 *

Resources