我有以下文件结构,每个文件最多有一行代码(如下所示):
a
├── b
│ ├── c.py import a.b.d as d
│ ├── d.py
│ └── __init__.py from a.b.c import *
├── __init__.py
└── main.py import a.b as b
通过运行python -m a.main,我得到以下错误:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line
我似乎做不到这件事。
我有两个python文件,比方说a.py和b.py
in a.py是这样的:
def foo():
global name
name = "foo"
import b
b.bar()
if __name__ == "__main__":
foo()
in b.py in:
import a
def bar():
name = a.name
print(name)
关于这个代码,我有三个不同的问题:
为什么我要得到错误:AttributeError: 'module
我有两个python文件file1.py和file2.py,在访问file1中的file1变量时,我得到了错误
file1.py包含:
from file2 import A
B = 10
print(A+B)
file2.py包含:
def func():
------ # ignore this function
------ # ignore this function
if '__name__'=='__main__':
A = 20
然后得到错误:
AttributeError:模块'file2‘没有属性'A’
如
>>> class Foo:
... 'it is a example'
... print 'i am here'
...
i am here
>>> Foo.__name__
'Foo'
>>> Foo().__name__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no
我有一项本来应该很简单的任务,这已经让我困惑了一段时间。我正在尝试patch一个导入到当前模块中的对象。
根据的答案
我应该可以patch("__main__.imported_obj")了。不过,这对我不起作用。请参阅我下面的最小repro (我正在通过pytest运行测试):
极小复制
这是使用Python 3.8.6运行的。
from random import random
from unittest.mock import patch
import pytest
@pytest.fixture
def foo():
with patch("__ma
这段代码在linux上执行,但抛出一个AttributeError:在windows上输入object 'T‘没有属性'val’,为什么?
from multiprocessing import Process
import sys
class T():
@classmethod
def init(cls, val):
cls.val = val
def f():
print(T.val)
if __name__ == '__main__':
T.init(5)
f()
p = Proce
为什么我得到这个错误--> AttributeError: module 'collections' has no attribute 'namedtuple'。我的代码出了什么问题.. 我在windows上使用的是python 3.5.2。当我在IDLE上执行代码时,它可以运行--显示kivy窗口,但当我执行C:\POS\operator\python operator.py时,它失败了。 from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Operator
我想知道是否有可能在不提供MyClass代码的情况下正确读取pickle文件。
import pickle
class MyClass:
def __init__(self, n):
self._n = n
def give_vec(self):
return [1 for _ in range(self._n)]
if __name__ == '__main__':
m = MyClass(30)
with open('test.pickle', 'wb') as p:
最近,我在Mac上重新安装了最新的Anaconda3,并在
from multiprocessing import Pool
def f(x):
return x*x
with Pool(5) as p:
p.apply(f, [1])
错误就像
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
但是同样的代码在几天前就起作用了!
python新手,正在尝试做一个随机数生成器。然而,我在导入随机模块时遇到了问题。当我尝试使用随机模块中的任何东西时,我都会得到一个AttributeError。谢谢你的帮助。
#!/usr/bin/python -tt
import random
def main():
x = random.randint(1,1000)
print x
if __name__ == '__main__':
main()
我正在设法在多个python脚本之间共享一个变量。我有以下代码:
b.py
my_variable = []
a.py
from b import my_variable # import the value
def run():
global x
x = my_variable.append("and another string")
print(my_variable)
if __name__ == '__main__':
run()
c.py
import a
print(a.x)
a.py运行良好
我有一个系统,它收集从某些基类派生的所有类,并将它们存储在字典中。我希望避免指定哪些类可用(我希望通过编程来发现它们),因此使用了from ModuleName import *语句。然后指示用户将所有要收集的测试放在ModuleName模块中。但是,我无法找到一种方法来编程地确定用该import语句导入了哪些符号。我尝试过使用dir()和__dict__,如下面的示例所示,但没有效果。如何通过编程找到以这种方式导入的符号(使用import *)?我无法用上述方法找到它们。
testTypeFigureOuterrer.py:
from testType1 import *
from test
我试图加载一个泡菜,并显示一些数据通过一个酒瓶应用程序。我所遵循的结构如下。
package1.py 1.py
class myclass:
#do something
m = myclass()
pickle.load(m,filehandle)
package2.py 2.py
from package1 import myclass
def query_processor(params):
x = pickle.load(filehandle)
#do stuff on x using params
return results
if __name__ =
我有这个问题。我需要将在类A中的execute ()方法中处理的"a“变量传递给位于不同文件中的类B中的execute ()方法。
下面是我的代码:
fileA.py
a = 0
class A:
def execute(self):
global a
a = 10
fileB.py
from fileA import A
class B:
def execute(self):
b = a
当在一个程序中序列化一个类(使用搁置)时,如果没有得到以下错误,我就无法检索它:
File "\Python36_64\lib\shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'foo'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "program.py", line