简体   繁体   English

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

[英]Very simple importing is not working in python

main.py:主要.py:

import second
from second import *

print(second.a)

second.py:第二个.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在项目文件夹中,还有其他.py文件以及__pycache__文件夹

Doing ↓ doesn't work either:做 ↓ 也不起作用:

from second import a, b

Your code works for me as well.你的代码也适用于我。 I think it maybe caused by your .pyc file which you run your code before.我认为这可能是由您之前运行代码的 .pyc 文件引起的。

Delete .pyc file in your package and rerun your code.删除包中的 .pyc 文件并重新运行代码。

Or you can try running your code in a new environment.或者您可以尝试在新环境中运行您的代码。

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!我感谢所有的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM