简体   繁体   English

在python中导入模块/脚本

[英]importing modules/scripts in python

So at work i'm developing a way for us to push out tools/plugins to the team as a whole. 因此,在工作中,我正在为我们开发一种向整个团队推出工具/插件的方法。 I actually have the system up and running and it's completely dynamic except for the topic i'm about to talk about (this is all done in python). 实际上,我已经启动并运行了系统,除了我要谈论的主题之外,它是完全动态的(这全部是在python中完成的)。 On start up Maya checks the local folder against a folder on the server and checks to see if they are different and handles and copying down files/dirs that are needed as well as the deleting of old plugins that we delete on the server. 在启动时,Maya会对照服务器上的文件夹检查本地文件夹,并检查它们是否不同,并处理和复制所需的文件/目录以及删除我们在服务器上删除的旧插件。 The system is flexible enough that users can create custom shelves of all the plugins and we can re organize the folders in the back end without breaking the shelves of all the users. 该系统足够灵活,用户可以创建所有插件的自定义架子,并且我们可以在不破坏所有用户架子的情况下重新组织后端的文件夹。 The plugins are accessed through a drop down menu in Maya's main interface and we can add sub folders into the system and plugins freely without messing with the code. 可通过Maya主界面中的下拉菜单访问插件,我们可以在不干扰代码的情况下将子文件夹免费添加到系统和插件中。 We can also arrange the order at which menu items and plugins can be displayed through a simple numbering system of the folders. 我们还可以安排通过文件夹的简单编号系统显示菜单项和插件的顺序。

This is all working fine until I get to making plugins, when they import a module in their folder, dynamic as well. 在我制作插件之前,这一切都很好,当它们将模块导入其文件夹时,也是动态的。 So when I start moving the plugins folder around the root directory, if I have an imported module that I created the path for, the imported modules path in the plugin script is now wrong at that point. 因此,当我开始在根目录周围移动plugins文件夹时,如果我有一个为其创建路径的导入模块,则此时插件脚本中的导入模块路径现在是错误的。 I already have a way of getting the proper path info to the plugin through my menu setup. 我已经有一种方法可以通过菜单设置来获取正确的插件路径信息。 I'm having issue with the import of the module and accessing classes with in that module. 我在导入模块和在该模块中访问类时遇到问题。

so If the standard for importing a module's class 所以如果导入模块类的标准

from fileName import className

and the __import__ way that i'm using looks like. 和我使用的__import__方式看起来像。

className = __import__("folderN.folderN.folderN.fileName", {}, {}, ["className"])

But with that method I loose the ability to just call on that class name like I can with the regular from import method. 但是使用该方法,我失去了像使用常规from导入方法那样仅调用该类名的能力。 I got around that by doing 我通过这样做解决了

className = className.className

but this is a rather ugly method and i'd prefer to be able to just import and call on the name without doing that extra step. 但这是一个相当丑陋的方法,我希望能够仅导入和调用名称,而无需执行此额外步骤。 I do not know this import method very well and I know i'm missing some things with it. 我不太了解此导入方法,并且我知道它缺少一些东西。

Am I just going about this import process the wrong way? 我会以错误的方式进行此导入过程吗? Is there a way to make it look into the local directory for the plugin without appending to maya's paths that way i can just do the regular way of importing method without a weird path that has to change anytime I move the plugin? 有没有一种方法可以使它查看插件的本地目录,而无需附加到maya的路径,这样我就可以执行常规的导入方法,而无需在每次移动插件时都需要更改的怪异路径?

__import__ doesn't work they way you are assuming. __import__不起作用,就像您假设的那样。 It returns a module object for the import path provided, with a guarantee that all the children you specify in the list have been explicitly imported. 它为提供的导入路径返回一个模块对象,并确保您在列表中指定的所有子级均已明确导入。 For classes, it doesn't really make a difference. 对于类,这并没有真正的区别。

mod = __import__('a.b', {}, {}, ['c', 'd'])

Is more accurately the equivalent of 更精确地相当于

import a.b
try:
    import a.b.c
except ImportError:
    pass
try:
    import a.b.d
except ImportError:
    pass
mod = a.b

What you actually probably want here is something like 您实际上可能想要的是这样的东西

child = getattr(__import__(import_path, {}, {}, [child_name]), child_name)

As to your versioning and distribution system, have you looked at using an SCM/VCS like SVN or GIT, and/or a package management system? 至于版本和分发系统,您是否考虑过使用SCM / VCS(例如SVN或GIT)和/或程序包管理系统? These give you much better change tracking and synchronization than a straight file share, and you could easily integrate them with a sync/install script on your client machines that could be customizable as needed for the client's specific configuration demands. 与直接的文件共享相比,它们为您提供了更好的更改跟踪和同步,并且您可以轻松地将它们与客户端计算机上的同步/安装脚本集成在一起,可以根据客户端的特定配置需求进行自定义。

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

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