[英]How can I import a python file through a command prompt?
I am working on project euler and wanted to time all of my code. 我正在研究项目euler,并希望计算我的所有代码。 What I have is directory of files in the form 'problemxxx.py' where xxx is the problem number. 我所拥有的是'problemxxx.py'形式的文件目录,其中xxx是问题编号。 Each of these files has a main()
function that returns the answer. 每个文件都有一个main()
函数返回答案。 So I have created a file called run.py, located in the same directory as the problem files. 所以我创建了一个名为run.py的文件,该文件与问题文件位于同一目录中。 I am able to get the name of the file through command prompt. 我可以通过命令提示符获取文件的名称。 But when I try to import the problem file, I continue to get ImportError: No module named problem
. 但是当我尝试导入问题文件时,我继续得到ImportError: No module named problem
。 Below is the code for run.py so far, along with the command prompt used. 下面是run.py到目前为止的代码,以及使用的命令提示符。
# run.py
import sys
problem = sys.argv[1]
import problem # I have also tired 'from problem import main' w/ same result
# will add timeit functions later, but trying to get this to run first
problem.main()
The command prompts that I have tried are the following: (both of which give the ImportError stated above) 该命令提示我尝试过以下内容:(两者都给出了上面描述的ImportError)
python run.py problem001
python run.py problem001.py
How can I import the function main()
from the file problem001.py? 如何从文件problem001.py导入函数main()
? Does importing not work with the file name stored as a variable? 导入不能与存储为变量的文件名一起使用吗? Is there a better solution than trying to get the file name through command prompt? 有没有比通过命令提示符获取文件名更好的解决方案? Let me know if I need to add more information, and thank you for any help! 如果我需要添加更多信息,请告诉我,谢谢您的帮助!
You can do this by using the __import__()
function. 您可以使用__import__()
函数执行此操作。
# run.py
import sys
problem = __import__(sys.argv[1], fromlist=["main"]) # I have also tired 'from problem import main' w/ same result
problem.main()
Then if you have problem001.py
like this: 然后,如果您有这样的problem001.py
:
def main():
print "In sub_main"
Calling python run.py problem001
prints: 调用python run.py problem001
打印:
In sub_main
A cleaner way to do this (instead of the __import__
way) is to use the importlib
module. 更__import__
方法是使用importlib
模块(而不是__import__
方式)。 Your run.py
needs to changes: 你的run.py
需要改变:
import importlib
problem = importlib.import_module(sys.argv[1])
Alternatives are mentioned in this question . 在这个问题中提到了替代方案。
For sure! 当然! You can use __ import_ built-in function like __import__(problem)
. 您可以使用__ import_内置函数,如__import__(problem)
。 However this is not recommended to use, because it is not nice in terms of coding-style. 但是不建议使用它,因为它在编码风格方面不是很好。 I think if you are using this for testing purposes then you should use unittest module, either way try to avoid these constructions. 我认为如果你将它用于测试目的,那么你应该使用unittest模块,无论哪种方式都试图避免这些结构。
Regards 问候
You can use exec() trick: 你可以使用exec()技巧:
import sys
problem = sys.argv[1]
exec('import %s' % problem)
exec('%s.main()' % problem)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.