Issues with Python environment variable

To start off, I have both Maya and Blender installed.
So, I needed a specific version of Python for a script in Blender so I had to install another version than what comes with Maya. After I did that I had to add an environment variable PythonPath with values for this new Python version. Otherwise Blender couldn’t find it.

Problem is, that once I did that, Maya wasn’t getting access to it’s Python. So I tried adding the folder path to C:…\Maya2019\bin, but there seems to be a conflict. I can’t have PythonPath pointing to different Python versions. :thinking:

So my solution is to add the environment variable for Python 2.6 whenever I need to run my script in Blender. Then delete the environment variable before starting Maya.

There has to be a solution to this problem…:disappointed_relieved:

Create a bat file for maya and blender and set the environment variable there. If you’re using linux or macos then create a bash script.

This is how you could do it for maya. You could do the same thing for blender or any other application.

@echo off

set PYTHONPATH=/some/random/path
"C:\Program Files\Autodesk\Maya%MAYA_VERSION%\bin\maya.exe" %*
1 Like

A lovely example of why env vars are the devil.

Two solid alternatives :

  1. just launch your apps from either a python script or a bat file as in @max_wiklund’s example, setting the environment you need right at launch time. That lets you run multiuple apps side by side with their own environments
  2. Each app’s startup script gets an explicit config step that will add files to its module search directoy – site.addsitedir is the easy route here. It can be as simple as a .pth file (which is part of site) or it can be more dynamic…though simple is better. In Maya you can run a python snippet at startup with maya.exe -c 'python(\\"your python here\\")
1 Like

Ah.
And here I thought env vars was the greatest things since sliced bread. :tipping_hand_man:

In alt. 2. Could you provide an example of what it would look like?

They are great if you build a launcher to maintain them for each app session. :wink:

I too have a strong distaste for pipelines that rely heavily on windows system/user env vars.

1 Like

the bare minimum would just be something like

maya.exe  -c "python(\"import site; site.addsitedir('path/to/my/stuff')\")"

Which would just make sure that `/path/to/mty/stuff’ was available inside that maya session. You could make an importable startup module or startup script in that location and then call it

maya.exe  -c "python(\"import site; site.addsitedir('path/to/my/stuff'); import my_startup_mod\")"

or you could just execute a single file and then have that file do all the fancy stuff:

maya.exe  -c "python(\"execfile('path/to/the/file.py')\")"

The only rough bit is the stupid string escapes

1 Like

Here is one more example of a Python file launching Maya.

1 Like

@kiryha’s example points out something I left out – you can launch maya in a subprocess and use the native ability of subprocess to manipulate environments to set the vars.