Importing Module issue (name of project and name of module are the same) - python-3.x

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.

Related

ModuleNotFoundError: No module named 'module' while importing my own module VsCode MacOs Python 3.10

Structure:
|--folder/
|--a.py
|--main.py
When loading module 'main' into module 'a'
#a.py
import main
the following error occurs - ModuleNotFoundError: No module named 'main'.
PyCharm copes with this task, but VSCode does not. What's the matter?
Looking at your directory structure, I presume that a.py and main.py are in different directories. If that is the case, then the answers to [this question][1] should be useful. To elaborate a bit further, you can use the sys module to specify the path to the other module (main.py) i.e. if main.py is in a different directory from a.py.
import sys
sys.path.append('/path_to_main_module_directory/')
import main
print("This import is for the main module")
I hope this helps!
[1]: Importing files from different folder

How to write the correct path for importlib in my pip installed python package?

I am trying to create a package where when I call a module a certain class from a python file is automatically loaded, but I can not figure out how to set the correct path with importlib. I try to load the package after installing it with pip install.
In the subfolder pkg_sub, I have __init__.py and testfile.py.
The __init__.py contains this code:
import importlib
pkclass = getattr(importlib.import_module('testfile'), 'pkclass')
And 'testfile.py' contains this code:
class pkclass:
# Some generic print commands for testing.
When I import pkg_sub, I get the error that importlib can not find testfile.py.
I am still confused with how pathing works. I also tried this line in __init__.py:
import importlib
pkclass = getattr(importlib.import_module('.testfile'), 'pkclass')
But then importlib wants to have a package directory in addition to testfile.py. But there are no additional subfolders that I could point importlib to.
Overall, I am just confused how I have to write the path here so that I can make the class directly accessible when I load the module. Any help would be appreciated.

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

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 *

setting sys.path to have the parent module path doesn't import the sub module

The project is in Python3:
Project
|
Mymodule
__init__.py
|
submodule1
__init__.py
some_script.py
|
submodule2
__init__.py
myclass.py -> implements MyCLass
Whenever I run a script, I start from my Project directory. I always execute from this Project location
In Mymodule.submodule1.some_script.py, I want to import MyClass inside some_script.py which is located in submodule2
Things that work in some_script.py
from ..submodule2.myclass import MyClass
from Mymodule.submodule2.myclass import MyClass
What I am trying to achieve is to avoid location based relative reference in my script file in my submodules. When ever I try to import a module It should search in the current directory, if not found, the go to sys.path.
So my solution is in my __init__.py of my submodule1
import os
import sys
sys.path.append(os.path.abspath('./Mymodule')) -> cwd will be "Project"
Now inside my some_script.py if I import
from submodule2.myclass import MyClass
This should work, because I have a sys.path entry to the root/parent module(Mymodule). I have verified this by print(sys.path) in some_script.py
This always throws an error:
ImportError: No module named 'submodule2.myclass
Why its not considering the sys.path to search for the module.
I was able to figure out the issue. In my sys.path, there was already a similarly named module(submodule2) in a different path way ahead of my current project path. So python was searching file in the first module that it encountered while searching for the module, which was in a different location
print(sys.path)
\somepath: ... : .... : ... :\myproject_path
\somepath -> has module(submodule2) with a similar name
\myproject_path -> has my module with a similar name submodule2

Resources