Maya - 如何使用多个文件创建 python 个脚本?

Maya - How to create python scripts with more than one file?

我是第一次post来这里,请理解我是初学者,我是在“在职”学习。

谁能解释一下如何从 Maya python 脚本中的不同模块导入文件?我收到以下错误:

Error: ImportError: file E:/.../bin/mainScript.py line 17: No module named tools

这是我的目录和代码:

Main_folder\
|-- install.mel
|-- ReadMeFirst.txt
`-- bin\
    |-- __init__.py
    |-- mainScript.py
    |-- tools.py
    `-- presets\
        |-- bipedPreset.txt
        |-- quadrupedPreset.txt
        `-- [...] .txt

我正在尝试在 mainScript.py

中导入 tools.py

编辑:

好的,因为它不适合评论,所以我编辑此 post 以增加精度。我在桌面上移动了 'Main_folder' 并在 Maya 中再次移动了 运行 脚本。它仍然不起作用,但我有一个更完整的错误回溯。这是:

# Error: Error in  maya.utils._guiExceptHook:
#   File "C:\Program Files\Autodesk\Maya2014\Python\lib\site-packages\maya\utils.py", line 332, in formatGuiException
#     result = u'%s: file %s line %s: %s' % (exceptionType.__name__, file, line, exceptionMsg)
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 11: ordinal not in range(128)
# 
# Original exception was:
# Traceback (most recent call last):
#   File "<maya console>", line 3, in <module>
#   File "C:/Users/UKDP/Desktop/Main_folder/bin/mainScript.py", line 17, in <module>
#     from tools import ClassTest
# ImportError: No module named tools # 

尝试像这样导入:

>>>import san.libs.stringops

Where the san is dir(in san create __init__.py)
libs is a dir(in libs create __init__.py) 
and stringops.py is imported 

您需要确保任何可导入的模块都在您的 python 路径上。

如果您的文件在 E:/main_folder 中,您需要执行

import sys
sys.path.append("E:/main_folder")
import bin.tools
import bin.mainScript

等等。你设置它的方式(使用 'bin/__init__.py')你告诉 python 该模块被命名为 bin 并且它有名为 'mainScript' 和 'tools' 的子模块.长时间的讨论 here

试试这个

我的桌面上有一个名为 Combo 的文件夹,并且有一个名为 combo.py 的脚本,这是我访问它的方式:

import sys #imports system's directory module
sys.path.insert(0,"C:/Users/dharmin.doshi/Desktop") #Add your directory, do not add your folder in the path unless the script is inside of folder inside of folder. Path needs to be in quotes and 0 is added as needed by the argument.
from Combo(Folder name) import combo (my python script)
reload (combo) #Optional to reload it every time you make changes to the script.
combo.run() #Call the function that runs the code. 

在你的情况下,如果你需要访问 tools.py 那么你的路径将是这样的:

sys.path.insert(0, "MainFolder/bin")
import tools

希望这对您有所帮助:)

exe1.py
import san.libs.stringops
import san.libs.maths
import san.libs.utils.ran
import san.printing

newstr = san.libs.stringops.add2string("hey","hi")
san.printing.myprint(newstr)

result = san.libs.maths.add(2,3)
san.printing.myprint(result)

random_number = san.libs.utils.ran.getnumber()
san.printing.myprint(random_number)