Python #错误:ImportError:没有名为samples.lightIntensity的模块#

Python #错误:ImportError:没有名为samples.lightIntensity的模块#,python,module,maya,Python,Module,Maya,这是我的剧本: import maya.cmds as cmds def changeLightIntensity(percentage=1.0): """ Changes the intensity of each light the scene by percentage. Parameters: percentage - Percentage to change each light's intensity. Default value is 1

这是我的剧本:

import maya.cmds as cmds

def changeLightIntensity(percentage=1.0):
    """
    Changes the intensity of each light the scene by percentage.

    Parameters:
        percentage - Percentage to change each light's intensity. Default value is 1.

    Returns:
        Nothing.
    """
    #The is command is the list command. It is used to list various nodes
    #in the  current scene. You can also use it to list selected nodes.
    lightInScene = cmds.ls(type='light')

    #If there are no lights in the scene, there is no point running this script
    if not lightInScene:
        raise RunTimeError, 'There are no lights in the scene!'

    #Loop through each light
    for light in lightInScene:
        #The getAttr command is used to get attribute values of a node
        currentIntensity = cmds.getAttr('%s.intensity' % light)
        #Calculate a new intensity
        newIntensity = currentIntensity * percentage
        #Change the lights intensity to the new intensity
        cmds.setAttr('%s.intensity' % light, newIntensity)
        #Report to the user what we just did
        print 'Changed the intensity of light %s from %.3f to %.3f' % (light, currentIntensity, newIntensity)


import samples.lightIntensity as lightIntensity
lightIntensity.changelightIntensity(1.2)
我的错误是:

ImportError:没有名为samples.lightIntensity的模块

有什么问题吗?我能处理这个吗?解决办法是什么

谢谢

看来您正在学习教程。您误解的是,代码示例中的最后两行不应该是脚本的一部分,但它们应该在解释器中运行前面的代码。如果您再看一看本教程,您将看到主代码示例上方有一个标题,标题是lightIntensity.py,而第二个较小的代码示例前面有“要运行此脚本,请在脚本编辑器中键入…”

那么这个,

import samples.lightIntensity as lightIntensity
lightIntensity.changelightIntensity(1.2)
不应以该形式出现在您的文件中

你可以做两件事。这两种方法都可以解决问题并允许您运行代码,不过为了便于使用,我更喜欢第二种方法

  • 将没有最后两行的代码保存为
    lightIntensity.py
    ,并保存在python shell中(启动python,在命令行上,空闲,无论您使用什么),在提示后,键入
    import lightIntensity
    导入脚本,然后键入
    lightIntensity.changelightIntensity(1.2)
    调用脚本中的函数

  • 或者,您可以修复脚本,使其在不尝试导入自身的情况下运行。为此,请删除
    import
    行,并将最后一行更改为
    changelightIntensity(1.2)


  • 我又错过了什么。因为我将脚本保存为lightIntensity.py,所以我运行了两行:import samples.lightIntensity作为lightIntensity.changelightIntensity(1.2),然后我得到了错误:ImportError:没有名为samples.lightIntensity的模块。出了什么问题(再次)?啊,对。实际上,
    import samples.lightIntensity
    只有在lightIntensity.py位于名为
    samples
    的包中时才起作用。只需导入光强度即可。