gRPC generated code for Python 3 not working - python-3.x

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

Related

Pyhton main failing import in sub package not when sub packes is run as main

Im quite new to python packages. I'm currently programming in Python 3+
I have a project structure with different small files in a folder called 'Libraries' and 1 python file called Main.py.
My project structure:
Project structure
I have the following import structure:
#Main.py
from Libraries.ATV320 import ATV320
x = ATV320()
.....
#ATV320.py
from ModbusSerial import SerialClient
class ATV320:
......
#ModbusSerial.py
import time
class SerialClient:
......
If I run SerialClient.py i get no errors, if i run ATV320.py I get no errors. When running main.py i get following error:
File "c:\Users\public\Documents\GitHub\Project\Main.py", line 2, in <module>
from Libraries.ATV320 import ATV320
File "c:\Users\public\Documents\GitHub\Project\Libraries\ATV320.py", line 1, in <module>
from ModbusSerial import SerialClient
ModuleNotFoundError: No module named 'ModbusSerial'
Anyone know what I am doing wrong? Since the two library files work fine when executed...
When i run main.py its suddenly throws an import error?

Import don't run in python

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.

Import script in sub directory not working... Blender Add-On

I have a folder structure like this:
C:
|_Blueprint
│ main.py
│
└───toolbox
blueprint_tools.py
When I run this code in Blender's scripting text page:
from toolbox.blueprint_tools import dimension, array, modify
I get an error in the console
Traceback (most recent call last):
File "\main.py", line 20, in <module>
ModuleNotFoundError: No module named 'toolbox'
Error: Python script failed, check the message in the system console
I swear, when I run this code (on other projects) outside of Blender this structure and code runs fine. But I get an error when running this script in Blender for testing out my Add-On.
If I compile the Blueprint folder into a .zip and install it everything works fine.... ??
I'm lost, could anyone please help me.
If I run the code like this: (added a . before toolbox for CWD)
from .toolbox.blueprint_tools import dimension, array, modify
Traceback (most recent call last):
File "\main.py", line 20, in <module>
ImportError: attempted relative import with no known parent package
Error: Python script failed, check the message in the system console
Both your error traces write File \main.py ... which means that Blender considers your main.py file to be in the root folder, knowing nothing about its real location in the file system hierarchy.
When you installed your structure as a zip file, you provided all necessary info to Blender.
Addendum:
Temporarily, during developing / debugging your add-on, you may add the full path (for finding your toolbox.blueprint_tools module) to the sys.path variable.
There are 2 possibilities how to do it:
Insert into your main.py file these commands (use the path to your parent folder of your toolbox folder, of course):
import sys
sys.path += [r"C:\Users\Davi\Documents\Python\PARENT_of_toolbox"]
before your statement
from toolbox.blueprint_tools import dimension, array, modify
command, or
Insert into your main.py file these commands (use the path to your toolbox folder, of course):
import sys
sys.path += [r"C:\Users\Davi\Documents\Python\PARENT_of_toolbox\toolbox"]
before your modified statement
from blueprint_tools import dimension, array, modify

ModuleNotFoundErro--but I was already importing it

I'm trying to run a cron job from my local machine using crontab. The file I'm trying to run is working without error when run manually from the command line.
The job looks pretty much like this:
01 13 * * * cd path/to/file && echo 'password' | sudo -S /usr/bin/python3 filename.py > log.txt 2>&1
Here's the error I was getting:
Password:Traceback (most recent call last):
File "contractsfinder.py", line 3, in <module>
from scraper_functions import *
File "</path/to/file>/my_functions.py", line 3, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
The file my_functions.py is just where I keep all my import requests in a central document accessed by a number of other python scripts. I am importing a number of modules. There is no issue with loading any of the modules when I am running this script manually.
I saw on another post somewhere that I should try to add: from urllib import requests which I did, and that got rid of the original error saying 'no module named requests'. However, now I'm getting a new error: ModuleNotFoundError: No module named 'bs4'
Trying the fix that worked for requests doesn't work for bs4 because there is no library to import it from, or at least I don't think there is. Previously I had just used import bs4.
So, obviously when I am running the file this way, something different needs to happen with the importing of modules. But when I did pip3 list earlier I saw both bs4 and requests were in there all along.
What do I need to do differently to get these modules to import correctly?

Imports not found when running script outside of Pycharm?

I have a project structured this way...
main.py imports scripts from subfolders like so:
from controllers.available_balances_controller import available_balances_controller
Subfolders:
models
views
controllers
When running main.py in Pycharm it works find.
When I try to run in terminal I get import errors:
Traceback (most recent call last):
File "main.py", line 6, in <module>
from controllers.available_balances_controller import available_balances_controller
ImportError: No module named controllers.available_balances_controller
Am I importing the scripts wrong in main.py?
What is the proper way to do the importing?
Try running your script with the -m flag:
$ python -m main
That means that you are running your main.py as a module inside a python package, not as a simple script. PyCharm makes it easy for you by assuming so when you create a project. When you are in the terminal, you need to specify it yourself. You don't need __init__.py files inside your directories in Python3.
Check out:
https://docs.python.org/3/reference/import.html
Relative imports in Python 3

Resources