非常简单的导入在Python中不起作用

发布于 2025-01-31 15:30:45 字数 428 浏览 3 评论 0原文

main.py:second.py

import second
from second import *

print(second.a)

a = "testA"
b = "testB"

错误:

AttributeError: module 'second' has no attribute 'a'

导入在更复杂的程序上没有工作。试图简单地测试它,并且它不起作用。

在项目文件夹中,还有其他.py文件以及__ pycache __文件夹

执行↓也不起作用:

from second import a, b

main.py:

import second
from second import *

print(second.a)

second.py:

a = "testA"
b = "testB"

Error:

AttributeError: module 'second' has no attribute 'a'

Importing wasn't working on a more complex program. Tried to test it simply and it's not working.

Inside the project folder, there are the other .py files as well as a __pycache__ folder

Doing ↓ doesn't work either:

from second import a, b

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2025-02-07 15:30:45

您的代码也对我有用。我认为这可能是您以前运行代码的.pyc文件引起的。

在软件包中删除.pyc文件,然后重新运行代码。

或者,您可以尝试在新环境中运行代码。

Your code works for me as well. I think it maybe caused by your .pyc file which you run your code before.

Delete .pyc file in your package and rerun your code.

Or you can try running your code in a new environment.

好倦 2025-02-07 15:30:45

我弄清楚了!

答案很简单,是创建虚拟环境。

以下帮助我:

video

yglwku_1kr_1kr0ln35vly41rxgsssssuekssuekryomdlkv8

I figured it out!

The answer was quite simple, and it was to create a virtual environment.

The following helped me:

VIDEO

STACK

I appreciate all the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文