Getting no-name-in-module on pylint when having duplicates names - python-3.x

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

Related

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.

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.

How do I import module from directory in python,linux

I am running a python script however I get the following error;
ImportError: No module named 'config.paths'; 'config' is not a package
My project folder looks like this
Hell/
hen/
config/
paths.py
abc/
def/
code.py
and this project is the directory of my ubuntu system :
/home/fisker/workspace/Hell
I am trying to execute code.py, but It should get paths.py from config folder. How do I do this?
I even tried using;
PYTHONPATH="/home/fisker/workspace/Hell/:$PYTHONPATH" python code.py
and
appending the following code in the beginning of code.py
import sys
sys.path.append('/home/fisker/workspace/Hell')
You need to transform config to a package in order to import it.
Just create a __init__.py file in config directory.
Example:
Hell/
hen/
config/
paths.py
init.py
In your code.py file you will need to:
from code import what_you_want

ModuleNotFoundError: cannot import local file

I have a module with multiple files structured like this:
/bettermod/
├── __init__.py
├── api.py
├── bettermod.py
├── errors.py
└── loggers.py
From bettermod.py, I'm trying to import two things:
a class called API from api.py
the whole errors.py file
For the first thing, it is quite easy, I just have to do this:
from .api import API
However, for importing the whole errors.py file, I'm encountering a problem; I'm trying to do like this:
from . import errors
which should work, according to this python documentation, but it's raising the following error:
File "/path/to/bettermod/bettermod.py", line 10, in <module>
from . import errors
ModuleNotFoundError: No module named 'bettermod'
Edit: when debugging, I found that __name__ was equal to bettermod.bettermod
From docs:
Note that relative imports are based on the name of the current module.
I cannot tell you what is wrong with certainty, but there is a smell: bettermod package has a bettermod module. You want to do from bettermod.bettermod import MyBetterClass? I doubt it. In Python files ARE namespaces, so choosing your file and directory names is also an API design. Just keep that in mind.
I suspect the problem is masked by the name collision. Try this. If you run python in bettermod directory and say import bettermod you are importing bettermod\bettermod.py. And . is relative to the bettermod.py module. Now try to run python in directory above bettermod package directory. It will work because now . resolves to bettermod package.
Try:
import mymodule
mymodule.__file__
This will tell what mymodule is. For packages, it will show path to __init__.py. This will help you to orient yourself. Also look up PYHTONPATH and how you can use it to make sure you are importing from the right path.

Resources