Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在制作 Python 文本助手。我正在尝试使用 execfile() 来运行问题。用于询问问题的文件和命令文件位于不同的目录中,因此我无法使用导入(文件名)。
我的问题是如何在我的 execfile 中运行多个部分,包括一个变量
这就是我所拥有的: execfile ('C:/hidden/assistant/AnsData/', variable, '.py')
如果您知道要导入的文件在哪里,就不必求助于execfile骇客。在最坏的情况下,你可以这样做:
execfile
import importlib import sys sys.path.insert(0, 'C:/hidden/assistant/AnsData/') # "variable" is a string naming your module without the .py extension answermodule = importlib.import_module(variable)