0

Short question:

How would I append an environment Python variable with a batch script? I want do the equal to:

import sys

sys.path.append( 'E:\whatever\locallyScriptFolder' )

but with a batch file? I'm a batch noob.

Longer pipeline question:

I need to setup a Python script pipeline in Maya. All the scripts are in our Perforce folder. Here are also the .bat file which copy the userSetup.py to the users local drive. This userSetup.py executes when Maya starts. From where I want to start script from the Perforce folder, but this folder have a different path for every user (some has it on the E drive and so on). What would be the best way to be able to get those scripts? Is it to append a environment variable?

Example:

C:\Users\nameOnUser\Documents\maya\2014-x64\scripts\ **userSetup.py**

X:\RandomPath\Scripts\ **scriptWantToCallFromUserSetup1.py**

X:\RandomPath\Scripts\ **scriptWantToCallFromUserSetup2.py**

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Ella
  • 18
  • 3

2 Answers2

0
set PYTHONPATH=X:\RandomPath\Scripts

Whatever is in PYTHONPATH will be added to sys.path when Python starts.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • So when I: `print sys.path` the X:\RandomPath\Scripts doesn't show up, and when I try to `import ExportScript` i get `No module named ExportScript`, even though it's in the path folder. It works if I use `sys.path.append('X:\RandomPath\Scripts')`. Though, when I use `print os.environ` the path do show up under `MAYA_SCRIPT_PATH`. I don't really understand enviroment paths. What am I doing wrong? Thanks! – Ella Oct 14 '14 at 12:36
  • You mean if you set PYTHONPATH before launching Python then the path you set in there does not appear in sys.path? As far as I know it should work; see here: http://stackoverflow.com/a/4855685/4323 – John Zwinck Oct 14 '14 at 12:58
0

@ JohnZwinck's answer should work for running python from a batch file - it will set the variable for the lifetime of the bat file and anything started by the bat file. However it's not permanent: when the bat file run is done the settings will revert to their original state. The same is true if you change sys.path or os.environ inside your python.

That said, it sounds from your description like you don't need to change the path in the batch file, you need to change it inside of userSetup.py. The best way to do that is to add a couple of lines to the master copy of userSetup.py in perforce like so:

import site
site.addsitedir("x:/path/to/perforce/directory") 
# you can add more folders the same way

In this version your batch file just copies the userSetup.py to the user's Maya directory and runs maya; userSetup.py sets up all the paths which Maya needs. This is better than batch files because (a) batch files are lame and (b) if you use the site module you can add lots of extra directories with .pth files.

Some more background on this topic:

theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thanks Theodox. This is what I wanted. The problem was that the path is relative for every users. I wanted a way that the batch file would save that relative path without having the user to do anything. I think my questing was a bit unclear - I was a bit unsure what i actually wanted to do. The solution I went with in the end was to copy with the batch file the userSetup.py and a script which let the user to set their relative path (which save that to an XML, and at startup then add that environment path). – Ella Oct 17 '14 at 09:43