pathing issues

74 views
Skip to first unread message

Todd Widup

unread,
Dec 6, 2013, 3:03:55 PM12/6/13
to python_in...@googlegroups.com
ive noticed this a few times and I am trying to track down a WHY

myPath = "someDirectoryPath"
os.environ["PYTHONPATH"] = "%s;%s" % (myPath, os.environ["PYTHONPATH"])
# works occassionally

sys.path.append(myPath)
# works every time

any ideas or suggestions as to why updating the environ wouldn't work all the time? 



--
Todd Widup
Creature TD / Technical Artist
to...@toddwidup.com
todd....@gmail.com
www.toddwidup.com

Justin Israel

unread,
Dec 6, 2013, 3:16:53 PM12/6/13
to python_in...@googlegroups.com
Because an active python interpreter process isn't making use of the PYTHONPATH environment variable after the fact. It is used on startup to ultimately derive the sys.path
There are a number of things that actually contribute to the final sys.path. 

Modifying the PYTHONPATH on os.environ would only have an effect on the environment of a subprocess.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk35R3O1iT%2BFK55-PKCOd5WKbizRWZM%2BBYrrxeigJzSK3WQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Todd Widup

unread,
Dec 6, 2013, 3:25:26 PM12/6/13
to python_in...@googlegroups.com
that was just an example.

we've done for MAYA_SCRIPT_PATH and for mel is was picking up just fine from the environ edit, but for an AE template file, it failed unless we appended the sys path.





For more options, visit https://groups.google.com/groups/opt_out.

Justin Israel

unread,
Dec 6, 2013, 4:53:36 PM12/6/13
to python_in...@googlegroups.com

Ya but MAYA_SCRIPT_PATH is an application specific environment variable that Maya is free to read it at any time.
PYTHONPATH is specific to python. If Maya is choosing to read it from the env after initialization, I would consider it undefined behavior.

http://docs.python.org/2/tutorial/modules.html#the-module-search-path
"sys.path is initialized from these locations:
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
After initialization, Python programs can modify sys.path."

So you can see that after a python interpreter has initialized , you should be using sys.path and not banking on modifying the env for python imports. Unless you are doing it so that subprocesses will inherit the modification, although that should really be done specifically by passing an env dict anyways.
Mel is a different story and follows the semantics defined by Maya.

Todd Widup

unread,
Dec 6, 2013, 5:20:19 PM12/6/13
to python_in...@googlegroups.com
these are all being editing within a Maya session... done within our userSetup.py until we shift it over to a powershell based maya launcher



For more options, visit https://groups.google.com/groups/opt_out.

Todd Widup

unread,
Dec 6, 2013, 5:21:31 PM12/6/13
to python_in...@googlegroups.com
I understand what you are saying, basically, if we are doing this after opening Maya, do it as a sys.path, otherwise, we can do how we are doing it

Justin Israel

unread,
Dec 6, 2013, 5:55:19 PM12/6/13
to python_in...@googlegroups.com

I would still only use sys.path in userSetup.py as for all I know its being exec'd into the already initialized python interpreter.
Maya.env might be the place for PYTHONPATH modifications.

Todd Widup

unread,
Dec 6, 2013, 6:04:48 PM12/6/13
to python_in...@googlegroups.com
no problem.  we are going the route of using a powershell launcher eventually that sets everything for the user.  but for now...that all helps, thanks Justin



For more options, visit https://groups.google.com/groups/opt_out.

Ævar Guðmundsson

unread,
Dec 15, 2013, 6:04:58 PM12/15/13
to python_in...@googlegroups.com

   Just make sure whatever edit you are doing to sys.path you do to os.environ[ 'PYTHONPATH' ] as well, that way if you have to transfer your environment whatever edit you have done to sys.path in the instance you are working in will ripple through with os.environ into whatever config is being written ( for example render farms who use a multiplier approach rather than a distribution approach need to know the environment you wish to render in, the modification to your sys.path won't be there but most likely a text file which accompanies the render containing the __dict__ from os.environ )

  Hope this helps.
Reply all
Reply to author
Forward
0 new messages