Message 1 of 5
To import *.py from sub directories?

Not applicable
04-19-2010
12:04 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Does someone know how to import multi sub directories with *.py and *.pyw files with python?
For exemple to import my mel script lib I am using the following mel script code :
Then I did a similar function with Python because I would like to switch on Python :
Problem is Python seems to dislike a full path dir with the "import" function...
Then I did some research with google and found that :
- stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
As they said I added an empty __init__.py in each directories I would like to import my python.
But nothing works :(...
In maya I am doing some test with :
but I have the following error :
Then... is that possible???
I would like to source py and pyw files in sub-directories from my "\scripts\_SAMA_LIB\PY"
Python scripts work well in the main scripts directory, but I would like to work with sub dir without to do a sys.path.append(dir) witll all the sub directories!
Really thank you for your help!
For exemple to import my mel script lib I am using the following mel script code :
global proc LoadLib (string $libsToLoad[])
{
string $dir = `internalVar -userScriptDir` + "_SAMA_LIB/MEL/";
for ($lib in $libsToLoad)
{
string $sourcedir = $dir + $lib + "/";
string $files [] = `getFileList -fs "*.mel" -fld $sourcedir`;
print $files;
for ($file in $files)
{
string $cmd = "source \"" + $sourcedir + $file + "\";";
eval $cmd;
}
}
}
Then I did a similar function with Python because I would like to switch on Python :
import maya.cmds as mc
def LoadLib (libsToLoad):
_dir = mc.internalVar(userScriptDir=1) + '_SAMA_LIB/PY/'
for lib in libsToLoad:
sourceDir = _dir + lib + "/"
files = mc.getFileList(fld=sourceDir)
for _file in files:
_file = sourceDir + _file
print _file
import _file
Problem is Python seems to dislike a full path dir with the "import" function...
Then I did some research with google and found that :
- stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
As they said I added an empty __init__.py in each directories I would like to import my python.
But nothing works :(...
In maya I am doing some test with :
import maya.cmds as mc
_dir = mc.internalVar(userScriptDir=1) + '_SAMA_LIB/PY/__init__'
print _dir
import _dir
but I have the following error :
# Error: No module named _dir
# Traceback (most recent call last):
# File "<maya console>", line 4, in <module>
# ImportError: No module named _dir #
Then... is that possible???
I would like to source py and pyw files in sub-directories from my "\scripts\_SAMA_LIB\PY"
Python scripts work well in the main scripts directory, but I would like to work with sub dir without to do a sys.path.append(dir) witll all the sub directories!
Really thank you for your help!