2

It's my first post here, please understand that I'm a beginner and that I'm learning "on-the-job".

Can someone explain how can I import files from a different module in a Maya python script? I'm getting the following error:

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

Here are my directories and codes:

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

I'm trying to import tools.py in mainScript.py

EDIT:

Ok, as it won't fit in a comment I edit this post to add precisions. I moved the 'Main_folder' on my Desktop and ran the script once again in Maya. It still doesn't work but I have a more complete error traceback. Here it is:

# 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 # 
Community
  • 1
  • 1
UKDP
  • 226
  • 5
  • 21
  • 3
    could you edit in the full error message for the `ImportError`? – derricw Sep 25 '15 at 16:10
  • That's it, sorry. :) – UKDP Sep 25 '15 at 16:16
  • try _import_ (http://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files has a lot of different ways to import) – R Nar Sep 25 '15 at 16:20
  • 2
    How are you running the script? Likely if your current directory isn't bin/, it won't be considered part of your python path – Alvaro Sep 25 '15 at 16:21
  • Well, I actually "install" the script in Maya with the install.mel file. It creates a button in Maya containing the following code: import imp UKDP_PokTaPok = imp.load_source('mainScript', E:/Projets/En_cours/Main_folder/bin/mainScript.py') UKDP_PokTaPok.mainScript.UI() – UKDP Sep 25 '15 at 16:27
  • Then you should really take a look at what Maya does and change the question to ask specifically "How to create Python scripts for Maya with more than one file" – Alvaro Sep 25 '15 at 16:59
  • What is your current directory when the program is running? – stark Sep 25 '15 at 17:02
  • Actually I created this 'install.mel' so I don't have to care where is the main_folder, the user simply drag'n'drop it into Maya and it sources the script in the same folder where the install is. – UKDP Sep 25 '15 at 17:32

4 Answers4

0

Try importing like:

>>>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 
Sanyal
  • 864
  • 10
  • 22
0

You need to make sure any importable modules are on you python path.

If your files are in E:/main_folder you'll need to do

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

and so on. The way you have it set up (with 'bin/__init__.py') you're telling python that the module is named bin and it has submodules named 'mainScript' and 'tools'. Long discussion here

joojaa
  • 4,354
  • 1
  • 27
  • 45
theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thank you! It worked! I just replaced the path by **os.path.dirname(__ file__)** so it would work anywhere. Thank you very much, again! :) – UKDP Sep 25 '15 at 23:50
0

Try this

I have a folder that is on my desktop and the folder is called Combo and has a script called combo.py and here is how I access it:

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. 

In your case if you need to access tools.py then your path will be like:

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

Hope this helps :)

Dharmin Doshi
  • 21
  • 1
  • 8
0
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)

1st step 2nd step

step3

step 4

Sanyal
  • 864
  • 10
  • 22
  • [link](http://stackoverflow.com/questions/32786300/maya-how-to-create-python-scripts-with-more-than-one-file/32794372#32794372) – Sanyal Aug 16 '16 at 12:55