3ds max python import module problem

3ds max python import module problem

har1sf0x
Advocate Advocate
6,970 Views
4 Replies
Message 1 of 5

3ds max python import module problem

har1sf0x
Advocate
Advocate

Hello there,

 

I am new to Python. In my need to learn more I am using pycharm to create a very simple project with two packages (tools and main). Both folders have __init__.py. Tools (the name is not actually 'Tools') has a .py file which has a def foo() which print 'something' and main has a .py file that imports Tools and runs Tools.foo(). In PyCharm everything works fine but when i run the main.py in max (using "python.executefile") it gives an error:
<type 'exception.ImportError'> No module named Tools

 

I have done a lot of internet research but i did not managed to find an answer. Any clue?

 

 

0 Likes
Accepted solutions (1)
6,971 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
Accepted solution

is the package or module directory location of  "Tools" in the environment PYTHONPATH so that 3dsmax can know its available?

You can check by saying:

 

import sys
for x in sys.path:
   print x

if it isn't, then add it in your windows environment variables.(preferred)

or temporarily you can test it and say

 

import sys
sys.path.append( r'C:/Location/Of/Tools' )

Also, i believe max2017 doesn't rely on running python through maxscript anymore, so you can do it directly in the Python environment.

Message 3 of 5

har1sf0x
Advocate
Advocate

Hello there,

 

Thnx a lot for your useful advise. It worked BUT:

 

I have to import all my packages as paths in 3ds max sys.path and if i do so I will have problem with name scope since i could have Tools01.main and Tools02.main. Is there a work around except of using unique names in a single or multiple packages? I think that this destroys the power of python using packages.

 

Thnx in advance

 

P.S. relative import unfortunately does not work (from .Tools01 import main etc.) error 'Attempted relative import in non-package'

0 Likes
Message 4 of 5

har1sf0x
Advocate
Advocate
It seams that you have to append the parent folder (project folder ) and then the import works fine. The only think is that you have to reload all modules if you use 3ds max interpreter... THNX A LOT DDelapena
0 Likes
Message 5 of 5

Anonymous
Not applicable

Yes, if you append the root path, then your namespaces will work fine. 

 

Your welcome.

0 Likes