Import don't run in python - python-3.x

I am developing a Django project and I am trying to run a test file but an error in the import occurre.
My folder hierarchy to be like the image:
see the image of function process_data inside of core.py
I am in the test directory trying to run my test file.
My import to be like the code:
from coordenadas.core import process_data
but, when I run my code an error is showed:
Traceback (most recent call last):
File "tests_class_moviment.py", line 1, in <module>
from coordenadas.core import process_data
ModuleNotFoundError: No module named 'coordenadas'
I am tryed use relative import
from .coordenadas.core import process_data
from .core import process_data
from ..coordenadas.core import process_data
from ..core import process_data
but the only way that no error is showed in pycharm is the
from coordenadas.core import process_data
Some idea how can I solve it?

On a partially unrelated note, absolute imports are definitely recommended over relative imports. PEP8:
Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that PEP 328 [7] is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.
As for the module error itself, adding an __init__.py file to the "coordenadas" directory will turn it into a module, which is exactly what you need.

Related

gRPC generated code for Python 3 not working

protoc was generating invalid code in Python 3.8. I found that it works in Python 3.6.
I followed the instructions as per: This fantastic blog
The command I ran was:
python -m grpc_tools.protoc --python_out=src/ --grpc_python_out=src/ --proto_path generated=./src/interfaces src/interfaces/ecg.proto
The first issue was the generated imports were not working. I changed the code as below, for the import to work, in pb2_grpc.py:
from . import ecg_pb2 as generated_dot_ecg__pb2
The imports were fine in Visual Code, but now the run-time error.
I have a couple of files in the "generated" folder. I also added an init.py into this folder that was supposed to expose the contents, as:
from .ecg_pb2 import *
from .ecg_pb2_grpc import *
At a peer level to this "generated" folder I have another file - server.py. I am trying to import "generated". It is only importing with a relative path: from .generated import ecg_pb2_grpc
However, while the import works with the relative path, the main in server.py is not getting invoked. Run time error -
(p36) C:\ECG\src>python ecg_server.py
Traceback (most recent call last):
File "ecg_server.py", line 6, in
from .generated import ecg_pb2
ModuleNotFoundError: No module named 'main.generated'; 'main' is not a package

Python Relative Imports but I copied the documentation

I read all the other SO posts about this and it either doesn't work or uses sys.path.append.
Below is a replica of the official documentation:
All other files not shown are empty
moduleA.py
from ..subB.moduleB import MyClass
moduleB.py
class MyClass:
def __init__(self):
pass
package/subB/__init__.py
from .moduleB import MyClass
Traceback from running moduleA.py
Traceback (most recent call last):
File "path\to\my\projects\folder\package\subA\moduleA.py", line 1, in <module>
from ..subB.moduleB import MyClass
ImportError: attempted relative import with no known parent package
File Structure
The Python docs unfortunately forget to mention that their example only works if you run your code in a very specific way using the '-m' switch. So you would have to do python -m subA.moduleA and you need to ensure that your current working directory is package. Otherwise it will fail.
If you don't like these restrictions (like me), I've created an experimental import library: ultraimport
It gives you more control over your imports and lets you do file system based imports.
In moduleA.py you could then write:
import ultraimport
MyClass = ultraimport('__dir__/../subB/moduleB.py', 'MyClass')
This will always work, no matter how you run your code or what is your current working directory. Also no need to change sys.path. It will actually go to the file system and load the very file you've specified.

Not able to load python custom module from another directory

I want to load a custom python module from another directory. This question also has been asked many times and I have followed lot of links, and I have a solution. However, it is not working for me. I am fairly new to Python and looks like I am making a simple mistake which I am not able to find.
Below is the hierarchy. I want to import 'extollo_keyvault.py' inside 'testCode.py'. I can see the path for 'extollo_keyvault.py' when I print 'sys.path', however, the script execution fails saying that unable to find that module
Code:
import os
import sys
path_to_extolloKeyvault_module = os.path.join(os.getcwd(), 'extollo_instance', 'hardened', 'extollo_keyvault.py')
sys.path.insert(0, path_to_extolloKeyvault_module)
import extollo_keyvault
Error:
Traceback (most recent call last):
File "c:/Users/manjug/source/repos/extollo-instance-march-31/extollo_instance/Instance/testCode.py", line 6, in <module>
import extollo_keyvault
ModuleNotFoundError: No module named 'extollo_keyvault'
Your current code includes /Instance.
Try this code:
import os
import sys
from pathlib import Path
path = Path(os.getcwd())
path_to_extolloKeyvault_module = os.path.join(path.parent.absolute(), 'extollo-instance-march-31', 'extollo_instance', 'hardened'
sys.path.insert(0, path_to_extolloKeyvault_module)
import extollo_keyvault

Can't import relative module

The file structure for a module im creating goes as follows:
PIV
| __init__.py
| base.py
| core.py
| exceptions.py
.gitignore
LICENSE
requirements.txt
But whenever I run a file like core.py, I get the following error:
Traceback (most recent call last):
File "c:/Users/ghub4/OneDrive/Desktop/Python-Image-and-Video-tools/PIV/core.py", line 33, in <module>
from . import base
ImportError: attempted relative import with no known parent package
The same thing happens when I run the __init__.py file. I'm not sure on what went wrong because all of the python files are in the same folder. Can someone clarify what's the problem and explain how I should fix it?
Import code for core.py file:
from __future__ import absolute_import
import sys
import os
from PIL import Image
import io
from . import base
from . import exceptions
(The __init__.py folder has the same relative imports as in the core file but also including: from . import core)
Based upon the two links you will given below, here is what needed for the problem to solve:
Relative Imports error rectified
No module named base
You need to import the package as this
from mymodule import some_useful_method
Sometimes, we get the no module error, in this case, we can import like this
from module_name.classname import some_useful_method

I can't import modules from other folders

I'm a bit new to the Python world. I'm using Python3 and having hard times with imports.
I was using PyCharm on Windows to write an application. Everything was working through the project until I switched to Linux and VS Code.
Now I can't use absolute imports for importing modules from other packages in the same project.
For example, I'd like to import from module cards all available card types.
I tested the classes and everything is ok. I'm only getting this issue with the import stuff.
The project structure:
/
|-cards
|-__init__.py
|-card.py
|-monster_card.py
|-spell_card.py
|-trap_card.py
|-ritual_card.py
|-deck
|-__init__py
|-deck.py
|-system
# This is the code in __init__.py in cads package
from . trap_card import TrapCard
from . spell_card import SpellCard
from . ritual_card import RitualCard
from . monster_card import MonsterCard
__all__ = [TrapCard, SpellCard, RitualCard, MonsterCard]
# The following line, for example, does not work from inside another package
# I'm trying to import the modules in cards from deck
from cards import TrapCard, MonsterCard, SpellCard, RitualCard
When I try to import packages from another folders I get this error message:
Traceback (most recent call last):
File "/root/git-repos/Yu-Gi-Oh/decks/deck.py", line 3, in
from cards import TrapCard, MonsterCard, SpellCard, RitualCard
ModuleNotFoundError: No module named 'cards'
When you call import *, python search the module from sys.path. You need to add your root dir into sys.path before call import stmt.
For your case, your root dir is /.
like:
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from cards import *
other way
Add file __init__.py into your root dir for make it become a module. Then change from cards import * to from .cards import *.

Resources