简体   繁体   English

如何通过命令提示符导入python文件?

[英]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.

相关问题 如何使用python通过命令提示符运行外部文件中的命令? - How to run commands in a external file through command prompt using python? 我如何通过命令提示符运行多个 python 文件 - How do i run multiple python files through command prompt 如何从命令提示符调用 python 文件内部的 function ? - How can I call a function inside of a python file from command prompt? 使用命令提示符时如何将 Python 脚本输出保存到文件? - How can I Save a Python Script output to a file when using Command Prompt? 如何通过python在命令提示符下设置变量? - How to set a variable in command prompt through python? 模块可以通过anaconda提示符导入,但不能通过python.exe - Module can import through anaconda prompt, but not through python.exe 如何使用 Python 访问命令提示符历史记录 - How can I access command prompt history with Python 如何在命令提示符下将 python 变量传递给进程 - How can I pass a python variable to a process in the command prompt 如何在命令提示符中运行 VSCode 中的文件 - How can I run a file in VSCode in the Command Prompt 无法在脚本中导入python模块,但可以在命令提示符下导入 - Unable to import python module in script, but can in command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM