将python文件导入另一个python文件会引发错误

问题描述 投票:0回答:1

我有一个py文件-file1.py,其中仅包含类似-]的变量>

name="sovith"
age=20

现在我有一个py文件-file2.py,我使用以下命令在其中导入该文件-

from file1 import *

现在,如果我运行此py脚本-file2,它可以正常工作。我想将同一脚本作为ansible剧本的一部分运行,但出现以下错误:

TASK [Run the python script]
***********************************************************************************************
***********************************************************************************************
*********************************
fatal: [automation_server]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 1,
"stderr": "Shared connection to 10.242.174.192 closed.\r\n", "stderr_lines": ["Shared connection
to 10.242.174.192 closed."], "stdout": "Traceback (most recent call last):\r\n  File
\"/home/bgnanasekaran/.ansible/tmp/ansible-tmp-1592297404.396444
183065994122809/file2.py\", line 8, in <module>\r\n    from file1 import
*\r\nModuleNotFoundError: No module named 'file1'\r\n", "stdout_lines": ["Traceback (most
recent call last):", "  File \"/home/bgnanasekaran/.ansible/tmp/ansible-tmp-1592297404.396444
183065994122809/file2.py\", line 8, in <module>", "    from file1 import *",
"ModuleNotFoundError: No module named 'file1'"]}

注意:我什至创建了一个空的init.py

并尝试了相同的操作,但错误仍然存​​在。有人可以帮我解决这个问题吗?

我有一个py文件-file1.py,其中仅包含-name =“ sovith” age = 20这样的变量。现在我有一个py文件-file2.py,我使用以下命令在其中导入该文件-从file1 import *现在...

python python-3.x ansible ansible-2.x
1个回答
0
投票

从.file1 import *使用。注意点。现在应该可以工作了。希望对您有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.