Python模块,名称中带有破折号或连字符( - ) [英] Python Module with a dash, or hyphen (-) in its name

查看:1239
本文介绍了Python模块,名称中带有破折号或连字符( - )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的python模块,名称中包含短划线,foo-bar.py

I have an existing python module with a dash in its name, foo-bar.py

我希望更改模块名称作为模块是共享的,我将不得不追查它使用的所有地方,以便我的特殊情况可行。

Changing the module name is something I would prefer to avoid as the module is shared, and I would have to chase down all the places it is used so that my special case will work.

有没有办法加载名称中包含的模块通常被禁止的' - '?

Is there a way to load a module whose name contains the typically forbidden '-'?

(我知道这不是最佳做法。但对于这种情况,我宁愿不重新设计并测试更大的我不认为我的公司主人会批准我花时间实施这样的改变。)

(I do understand that this isn't a best practice. But for this situation I would prefer not to redesign and test a much larger set of applications. Also I don't think my corporate masters would approve of my taking the time to implement such a change.)

推荐答案

你可以使用 __ import __() 。例如:

You can do that using __import__(). For example:

foobar = __import__("foo-bar")

但你真的应该重命名模块。这样,您可以避免混淆模块的文件名与程序中使用的标识符不同。

But you really should rename the module instead. That way you can avoid confusion where the filename of the module is different from the identifier used in the program.

这篇关于Python模块,名称中带有破折号或连字符( - )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆