简体   繁体   English

从变量文件导入的Python

[英]Python from variable file import

I am new to python i want to use 我是想使用python的新手

from variablefile import * 变量文件导入*

i need to take variablefile as a input from user.i tried to use raw_input() but it didn't work, is there any way in python to do this? 我需要将variablefile作为user.i的输入,我尝试使用raw_input(),但没有用,python中有没有办法做到这一点?

from ... import * is not really good practice as it can cause name conflicts and makes it harder to see where classes etc. are really defined. from ... import *不是真正的好习惯,因为它可能导致名称冲突,并使很难看到类等的定义位置。 This would especially be the case if you are accepting ... from the user input 如果您从用户输入中接受... ,则尤其如此...

import importlib
globals().update(vars(importlib.import_module(variablefile)))

Try: 尝试:

myvar = "os"
eval("import %s" % myvar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM