简体   繁体   English

Python:如何从两个模块导入具有相同名称的类?

[英]Python: How to import, from two modules, Classes that have same names?

I'm writing a python programm to do granular syncs between different DB. 我正在编写一个python程序来在不同的DB之间进行粒度同步。

I'm using SQLAlchemy and a module named sqlautocode for DB inspecting and Schema Classes production. 我正在使用SQLAlchemy和一个名为sqlautocode的模块进行数据库检查和模式类生产。

Having two DB to sync, with same tables name, the Classes written by sqlautocode results with same names. 有两个DB同步,具有相同的表名,sqlautocode编写的类具有相同的名称。

I have to import theese Classes with arbitrary prefixes, I'm thinking about something like this: 我必须导入带有任意前缀的theese Classes,我正在考虑这样的事情:

from module_name import * with prefixes

Otherwise I should import every Classes name with an "as" modifier, like this: 否则我应该使用“as”修饰符导入每个Classes名称,如下所示:

from module_name import x as master_x

First of HardCode name extraction procedures with control lists and exec/eval complex code I'd like some suggestions about it. 第一个带有控制列表和exec / eval复杂代码的HardCode名称提取程序我想对它提出一些建议。

UPDATE: The solution is a sqlautocode option: --table-prefix=TABLE_PREFIX 更新:解决方案是sqlautocode选项: - table-prefix = TABLE_PREFIX

thank you all 谢谢你们

Just import the modules and don't try to pull the names from them. 只需导入模块,不要试图从中提取名称。 from X import Y should be used sporadically, anyway. from X import Y应该偶尔使用,无论如何。

import module_a
import module_b

module_a.x
module_b.x

You don't have to import classes from modules in this case. 在这种情况下,您不必从模块导入类。 Instead import both modules. 而是导入两个模块。 Like this: 像这样:

import moda, modb

moda.MyClass()
modb.MyClass()

On the other hand this doesn't let you use cool tools like pyflakes . 另一方面,这不允许你使用像pyflakes这样的酷工具。 So I would do the hard work (you have to do it only once, right?) and import classes with renaming. 所以我会努力工作(你必须只做一次,对吗?)并导入重命名的类。 I am great supporter of from x import something because it let's you detect errors the earliest possible. 我非常支持from x import something因为它让你尽可能地发现错误。

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

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