my folder structure is,
├── handler
│ ├── s3_handler.py
│
└── service
├── service.py
s3_handler file contains many functions which i need to use in service.py
I was trying
from handlers.s3_handler import *
but there is an error.
Traceback (most recent call last):
File "field_mapping_util.py", line 3, in <module>
from handlers.s3_handler import *
ModuleNotFoundError: No module named 'handlers'
how to resolve this
i figured it out.
inside service.py insert,
import sys
sys.path.append("..")
from handler.s3_handler import *
Related
I am writing a python 3.10 package with the following architecture:
pyce
├── docs
├── README.md
├── requirements.txt
├── setup.py
└── src
└── pyce
├── __init__.py
├── plot
│ ├── __init__.py
│ └── colormaps_lc.py
└── tools
├── __init__.py
└── lcmapping.py
All __init__.py files are empty. I have written the associated setup.py file:
from setuptools import find_packages, setup
setup(
name="pyce",
version="1.0",
packages=find_packages(where="src/"),
package_dir={"": "src"},
)
The problem I have is:
import pyce.tools as pytools
pytools.lcmapping
returns
AttributeError Traceback (most recent call last)
Cell In[1], line 2
1 import pyce.tools as pytools
----> 2 pytools.lcmapping
AttributeError: module 'pyce.tools' has no attribute 'lcmapping'
Similar issue if I do:
import pyce.tools as pytools
from pytools import lcmapping
returns
ModuleNotFoundError Traceback (most recent call last)
Cell In[2], line 2
1 import pyce.tools as pytools
----> 2 from pytools import lcmapping
ModuleNotFoundError: No module named 'pytools'
This is typically the issue mentioned here :
#the-submodules-are-added-to-the-package-namespace-trap
Also similar to this Stack 74487179
First solution
I tried from pyce.tools import lcmapping or import pyce.tools.lcmapping as lcmap and this works fine!
However I would like not to have to import every single module at the beginning of a script but rather do import pyce.tools as pytools and then do pytools.lcmapping along my scripts (with in mind the fact that I will have many modules in the sub-package).
Second solution
In each __init__.py files of my packages/sub-packages, import my sub-package/modules (respectively).
Detailed here: #packages, and it works!
Expected solution
I do not remember having this issue with python 3.7, the sub-packages/sub-modules were automatically found even with empty __init__.py files.
Would anyone know if I made a mistake in my setup.py file and/or project architecture? Or does this issues has something related to python 3.10 ?
Many thanks
I am coding an application that has 2 services integrated with rabbitmq(publisher and consumer).
I have a folder lib that contains a class manager_mq, that is used to manage the process of pub/sub of my application.
My structured the directory to be like the image:
In my consumer or publisher .py file, When I try to import manager_mq an error is returned.
My import to be like that:
from services.lib.manager_mq import Manager
However, an error is returned on the console.
Traceback (most recent call last):
File "consumer.py", line 5, in <module>
from services.lib.manager_mq import Manager
ModuleNotFoundError: No module named 'services'
I am tried to use relative import but the error occurs yet.
Traceback (most recent call last):
File "consumer.py", line 5, in <module>
from ..lib.manager_mq import Manager
ImportError: attempted relative import with no known parent package
How can I solve this problem?
Here both absolute and relative imports won't work. For more information, check out Importing from Parent Directory.
In this case it would probably be the easiest to use sys.path to add the lib to the path.
application
├── consumer
│ └── __init__.py
│ └── consumer.py
└── lib
└── __init__.py
└── manager_mq.py
manager_mq.py test code:
class Manager:
def __init__(self) -> None:
print('test')
consumer.py test code:
import sys
sys.path.append("../lib")
from manager_mq import Manager
m = Manager()
I am unable to import module from a different package.
The module connection.py is in a package instance_connector and the module record_parameter.py is in a different package called instance_parameters.
Both of these packages are the sub-packages of a package called snowflake.
Here is the tree diagram of directory structure.
snowflake
├── __init__.py
├── instance_connector
│ ├── __init__.py
│ └── connection.py
└── instance_parameters
├── __init__.py
├── load_parameters.py
├── modals.py
└── record_parameters.py
I am trying to import a module connection.py into module record_parameter.py like this -
record_parameter.py
from snowfake.instance_connector.connection import SnowflakeConnector
When I run this file via terminal using command python record_parameter.py it returns me an error stated below -
Traceback (most recent call last):
File "record_parameters.py", line 3, in <module>
from snowflake.instance_connector.connection import SnowflakeConnector
ModuleNotFoundError: No module named 'snowflake.instance_connector'
Where am I going wrong?
Have you tried appending the path which leads to the file connection.py using the sys module in record_parameters.py?
import sys
sys.path.append(1, 'xyz/snowflake/instance_connector/connection.py')
I was working on a project where I was training a neural network and displaying information using a GUI in Python. However, I have a constant problem with importing modules. This is the error I get when trying to run Visualizer.Main
Traceback (most recent call last):
File "/home/user/SplineTrajectoryGenerator/Visualizer/Main.py", line 3, in <module>
from Visualizer.Field import Field
File "/home/user/SplineTrajectoryGenerator/Visualizer/Field.py", line 3, in <module>
from Visualizer.Utils import loadImage
File "/home/user/SplineTrajectoryGenerator/Visualizer/Utils.py", line 3, in <module>
from NeuralNetworks.Pose import Pose2D
File "/home/user/SplineTrajectoryGenerator/NeuralNetworks/Pose.py", line 4, in <module>
from Visualizer.Utils import constraint
ImportError: cannot import name 'constraint'
However, the strange thing is that the variable exists and can be found in Utils.py in the Visualizer folder. I was told that using the _init__.py
allows a directory to be used as a module to import methods and classes from scripts in other directories. I'm still confused about why this error happens when the variable exists. For reference the tree of all the files look like this.
.
├── NeuralNetworks
│ ├── __init__.py
│ ├──Main.py
│ └──Pose.py
├──README.md
└── Visualizer
├── Assets
│ ├── Field.png
│ └── Robot.png
├──Field.py
├── __init__.py
├──Main.py
├──Robot.py
└──Utils.py
In case you need to see the scripts, I have attached the whole folder below with this link. Any help would be greatly appreciated.
You seem to have a cyclic dependency between Visualizer.Utils and NeuralNetworks.Pose`:
from Visualizer.Utils import loadImage
File "/home/user/SplineTrajectoryGenerator/Visualizer/Utils.py", line
3, in
from NeuralNetworks.Pose import Pose2D
File "/home/user/SplineTrajectoryGenerator/NeuralNetworks/Pose.py",
line 4, in
from Visualizer.Utils import constraint
Python does not support cyclic dependencies - and that's actually a GoodThing(tm) since those are design issues (two modules should not depend on each other). The canonical solution is to create a new module for objects (classes, functions, whatever) that are necessary to both modules (ie in your case create a distinct module for 'constraint').
directory structure
test/
__init__.py
line.py
test.py
test.py
from . import line
output:
Traceback (most recent call last):
File "test.py", line 1, in <module>
from . import line
ImportError: cannot import name 'line'
I know I can just import line. but it may import standard library in python3.
why did this error happen? does python3 support this syntax?
ps: I'm not in interactive console and test directory has already been a package
I'm not sure if you can import module with the same name as library utilities directly. However, you could consider putting your modules inside a package, so your directory structure would look like:
.
├── main.py
├── package
│ ├── __init__.py
│ ├── line.py
│ ├── test.py
Where __init__.py might have some setup commands (you can omit this file if you have none) and in main.py you can do the following:
from package import line
...
Within the directory package if you'd like to say, import line.py in test.py you can use the syntax:
from . import line
Note relative imports (using the from . notation) will only work inside the script and not in the interactive console. Running the test.py ( if it's using relative imports) directly will also not work, although importing it from main.py will work.