How to import one submodule from different submodule? [duplicate]

2024/11/15 21:07:47

My project has the following structure:

DSTC/st/__init__.pya.pyg.pytb.pydstc.py

Here is a.py in part:

import inspect
import queue
import threading

Here is tb.py in part:

import functools
from . import a

When run directly, a.py produces no errors, and it is easy to verify there were no SyntaxErrors. However, running tb.py causes the following error:

"C:\Program Files\Python36\python.exe" C:/Users/user/PycharmProjects/DSTC/st/tb.py
Traceback (most recent call last):File "C:/Users/user/PycharmProjects/DSTC/st/tb.py", line 15, in <module>from . import a
ImportError: cannot import name 'a'Process finished with exit code 1

How should I rewrite the import of a from tb so that tb can be run directly without causing errors?

Answer

Either you can use

import a

or relative

from .a import *

and in this case module **a** should be loaded

python -m a tb.py

will works for you.

import * is discouraged, import just as you need

If you got a main.py in your DSTC as follows:

#  main.py
from st import tb

and you run main.py only relative approach will work for you

# tb.py 
import a  # will not work
from .a import * # will work

because this time you load 'a' as a module.

https://en.xdnf.cn/q/71366.html

Related Q&A

How to add dimension to a tensor using Tensorflow

I have method reformat in which using numpy I convert a label(256,) to label(256,2) shape. Now I want to do same operation on a Tensor with shape (256,)My code looks like this (num_labels=2) :--def ref…

Down arrow symbol in matplotlib

I would like to create a plot where some of the points have a downward pointing arrow (see image below). In Astronomy this illustrates that the true value is actually lower than whats measured.Note tha…

Overwrite the previous print value in python?

How can i overwrite the previous "print" value in python?print "hello" print "dude" print "bye"It will output:hello dude byeBut i want to overwrite the value.In…

pyQt4 - How to select table rows and disable editing cells

I create a QTableWidget with:self.table = QtGui.QTableWidget() self.table.setObjectName(table) self.table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) verticalLayout.addWidget(self.table)wi…

Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)

How to fix the input array to meet the input shape?I tried to transpose the input array, as described here, but an error is the same.ValueError: Error when checking input: expected dense_input to have…

Sort order when loading related objects using selectinload in SQLAlchemy

Is there a way to specify the sort order when loading related objects using the selectinload option in SQLAlchemy?My SQLAlchemy version: 1.2.10 My python version: 3.6.6

How to implement autovivification for nested dictionary ONLY when assigning values?

TL;DR How can I get superkeys to be autovivified in a Python dict when assigning values to subkeys, without also getting them autovivified when checking for subkeys?Background: Normally in Python, se…

How can I iterate across the photos on my connected iPhone from Windows 7 in Python?

When I connect my iPhone to my Windows 7 system, the Windows Explorer opens a Virtual Folder to the DCIM content. I can access the shell library interface via Pywin32 (218) as mentioned here: Can I use…

Why doesnt the python slice syntax wrap around from negative to positive indices?

I noticed, given l = [1,2,3], that l[-1:] returns [3] as expected, but that l[-1:0] returns [], very much unlike what I expected. I then tried [-1:1], which I expected to return [3,1], but it also retu…

How do I modify a generator in Python?

Is there a common interface in Python that I could derive from to modify behavior of a generator?For example, I want to modify an existing generator to insert some values in the stream and remove some…