Python 如何使用已导入的变量?

Python 如何使用已导入的变量?,python,import,Python,Import,我正在尝试从另一个模块导入变量。我希望使用“导入模块”方式,而不是“从x导入y”方式。 导入行可以工作,但在尝试从源模块打印变量时出错 我有一个空的init.py文件;所有文件init、module1和module2都位于同一文件夹中。该文件夹显示在sys.path中。 使用从x导入y工作。我只想使用导入模块。 我错过了什么 模块1.py: X=8 List=[8,2,9] ListOfStrings=["Champa","Lampa", "Dampa"] All=[X, List, ListO

我正在尝试从另一个模块导入变量。我希望使用“导入模块”方式,而不是“从x导入y”方式。 导入行可以工作,但在尝试从源模块打印变量时出错

我有一个空的init.py文件;所有文件init、module1和module2都位于同一文件夹中。该文件夹显示在sys.path中。 使用从x导入y工作。我只想使用导入模块。 我错过了什么

模块1.py:

X=8
List=[8,2,9]
ListOfStrings=["Champa","Lampa", "Dampa"]
All=[X, List, ListOfStrings, String]

print(All)\
import module1
import sys
for p in sys.path:
    print(p)

print(X)
[8, [8, 2, 9], ['Champa', 'Lampa', 'Dampa'], 'This is a string']
theactualpath\Desktop\Work Excercises\py_test
Traceback (most recent call last):
  File "theactualpath\Desktop\Work Excercises\py_test\module2.py", line 6, in <module>
    print(X)
NameError: name 'X' is not defined
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "theactualpath\Desktop\Work\Excercises\py_test\module2.py"]
[dir: theactualpath\Desktop\Work Excercises\py_test]
[path: various paths from my computer, not the current working folder thou]
module2.py:

X=8
List=[8,2,9]
ListOfStrings=["Champa","Lampa", "Dampa"]
All=[X, List, ListOfStrings, String]

print(All)\
import module1
import sys
for p in sys.path:
    print(p)

print(X)
[8, [8, 2, 9], ['Champa', 'Lampa', 'Dampa'], 'This is a string']
theactualpath\Desktop\Work Excercises\py_test
Traceback (most recent call last):
  File "theactualpath\Desktop\Work Excercises\py_test\module2.py", line 6, in <module>
    print(X)
NameError: name 'X' is not defined
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "theactualpath\Desktop\Work\Excercises\py_test\module2.py"]
[dir: theactualpath\Desktop\Work Excercises\py_test]
[path: various paths from my computer, not the current working folder thou]
模块1运行,但X显示为未定义

结果:

X=8
List=[8,2,9]
ListOfStrings=["Champa","Lampa", "Dampa"]
All=[X, List, ListOfStrings, String]

print(All)\
import module1
import sys
for p in sys.path:
    print(p)

print(X)
[8, [8, 2, 9], ['Champa', 'Lampa', 'Dampa'], 'This is a string']
theactualpath\Desktop\Work Excercises\py_test
Traceback (most recent call last):
  File "theactualpath\Desktop\Work Excercises\py_test\module2.py", line 6, in <module>
    print(X)
NameError: name 'X' is not defined
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "theactualpath\Desktop\Work\Excercises\py_test\module2.py"]
[dir: theactualpath\Desktop\Work Excercises\py_test]
[path: various paths from my computer, not the current working folder thou]
[8、[8,2,9],“Champa”、“Lampa”、“Dampa”],“这是一个字符串”]
实际路径\桌面\工作练习\py\U测试
回溯(最近一次呼叫最后一次):
文件“theactualpath\Desktop\Work Exercises\py\u test\module2.py”,第6行,在
打印(X)
名称错误:未定义名称“X”
[在0.1s内完成,退出代码为1]
[shell\u cmd:python-u“theactualpath\Desktop\Work\exercises\py\u test\module2.py”]
[dir:theactualpath\Desktop\workexercises\py_test]
[路径:来自我的计算机的各种路径,而不是当前工作文件夹]

您有两种选择

引用module1命名空间:

import module1
...
print(module1.X)
from module1 import * # or just import  whatever you need: from module1 import X
...
print(X)
将module1中的所有内容(或您需要的任何内容)都带到module2命名空间中:

import module1
...
print(module1.X)
from module1 import * # or just import  whatever you need: from module1 import X
...
print(X)

大家好,欢迎来到StackOverflow

导入模块
import module1
后,变量
X
绑定到模块的名称空间,因此必须替换

print (X)


第一个示例(
print(X)
)将从当前文件而不是模块中打印变量
X

使用
print(X)
是错误的。您必须使用
print(module1.X)

您尚未导入
module1
@Netwave我导入了,很抱歉,我在这里设置问题格式时遇到问题,第一行没有显示。感谢您的欢迎和澄清!很高兴来到这里