当前位置: 首页 > news >正文

模板网的图片侵权/亚马逊关键词优化怎么做

模板网的图片侵权,亚马逊关键词优化怎么做,怎么做营销推广,在线客服源码我想根据用户选择的值导入一些包。默认值为file1.py:from files import file1如果用户选择file2,则应为:from files import file2号在PHP中,我可以使用变量:$file_name file1;include($$file_name);$file_name file2…

我想根据用户选择的值导入一些包。

默认值为file1.py:

from files import file1

如果用户选择file2,则应为:

from files import file2

在PHP中,我可以使用变量:

$file_name = 'file1';

include($$file_name);

$file_name = 'file2';

include($$file_name);

我怎样才能在python中做到这一点?

别这样!使用输入通常是不可信的,并且基于它盲目地评估代码是一个坏主意。

谢谢,我现在使用默认设置导入。

python没有直接等同于php的"变量"的特性。要获得"变量"的值(或任何其他表达式的值),可以使用eval函数。

foo ="Hello World"

print eval("foo")

但是,这不能用在import语句中。

可以使用__import__函数使用变量导入。

package ="os"

name ="path"

imported = getattr(__import__(package, fromlist=[name]), name)

等于

from os import path as imported

旧线索,但我需要答案,所以其他人可能…

在python 2.7+中有一种更简单的方法可以做到这一点:

import importlib

my_module = importlib.import_module("package.path.%s" % module_name)

小贴士:你需要一个package.path.__init__.py才能导入工作。

正如Fredrik Lundh所说:

Anyway, here’s how these statements and functions work:

import X imports the module X, and creates a reference to that module

in the current namespace. Or in other words, after you’ve run this

statement, you can use X.name to refer to things defined in module X.

from X import * imports the module X, and creates references in the

current namespace to all public objects defined by that module (that

is, everything that doesn’t have a name starting with"_"). Or in

other words, after you’ve run this statement, you can simply use a

plain name to refer to things defined in module X. But X itself is not

defined, so X.name doesn’t work. And if name was already defined, it

is replaced by the new version. And if name in X is changed to point

to some other object, your module won’t notice.

from X import a, b, c imports the module X, and creates references in

the current namespace to the given objects. Or in other words, you can

now use a and b and c in your program.

Finally, X = __import__(‘X’) works like import X, with the difference

that you 1) pass the module name as a string, and 2) explicitly assign

it to a variable in your current namespace.

顺便说一下,这是你最不感兴趣的方法。

简单地写(例如):

var ="datetime"

module = __import__(var)

谢谢你的回答清楚明了。

让用户选择要导入的内容可能是一个非常糟糕的主意。包可以在导入时执行代码,因此您实际上允许用户在系统上任意执行代码!这样做更安全

if user_input == 'file1.py':

from files import file1 as file

elif user_input == 'file2.py':

from files import file2 as file

else:

file = None

print"Sorry, you can't import that file"

根据mattjbray的回答:

from importlib import import_module

# lookup in a set is in constant time

safe_names = {"file1.py","file2.py","file3.py", ...}

user_input = ...

if user_input in safe_names:

file = import_module(user_input)

else:

print("Nope, not doing this.")

保存几行代码,并允许您以编程方式设置safe_names,或者加载多个模块并将它们分配给dict。

相关文章:

  • 本地网站SEO怎么做?如何做本地关键字研究
  • 网站中怎么做下载链接/北京seo优化wyhseo
  • 郑州网站建设规划/百度关键词价格怎么查询
  • 怎么建立自己网站 asp/seo培训多少钱
  • 淘宝联盟怎么做网站/推广广告
  • 怎么更改自动目录的格式/seo优化方式包括
  • 佛山网站建设4-win方维/seo关键词排名优化怎么收费
  • 怎么做微拍网站/郑州seo外包公司哪家好
  • 小店怎么做网站/汕头百度网站推广
  • 买了阿里云怎么做网站/兰州seo实战优化
  • 充值网站怎么做/seo兼职接单平台
  • 游戏交易类网站seo怎么做/网络销售平台怎么做