如图,我想用python 运行a.py 然后引入b.py 打印b.py 中的test1

运行文件python a.py
from . import b
print test1
b.py代码
test1=u'ssssssaaaawwwww;;llllll'
报错

或者想引入当前目录下的c.py也一样报错.请问大神哪里写错了,应该怎么写才对?
a.py代码:
from .dir import c
print test2
c.py代码:
test2=u'ssssssaaaawwwww;;llllll'
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
from b import test1or
import bfrom dir import cor
from dir.c import test2因为你是直接执行a.py, a.py是
__main__, 这时b与a就不是同级关系了, 所以from . import b就报错了.详细python import机制请看https://neo1218.github.io/pyt...
相对路径导入模块或者包只能在一个包里面才可以。
如果不是在包里面导入模块或者包,直接写模块名或者是包名就可以了
给提主说一下模块怎么导入吧
整个模块导入,在一个路径下用import b就可以了,不需要from . import b
from ... import ...是导入模块内的一部分,例如from b import test1,那么只导入b.py这个模块下的test1,如果模块里还有test2,test3什么的,就不导入了
看报错有可能是不允许import变量值?通常导入的是类或者函数。
dir下__init__.py
同级目录引用直接
improt 文件名就可以啊!即import b