2

I'm trying to set up virtualenv to point at Maya 2013's executable so I can run unit tests inside it from the shell. Maya uses a custom python 2.6 executable located at /usr/autodesk/maya/bin/mayapy. I get these errors:

$ virtualenv -p /usr/autodesk/maya/bin/mayapy mayapy
Running virtualenv with interpreter /usr/autodesk/maya/bin/mayapy
PYTHONHOME is set.  You *must* activate the virtualenv before using it
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 2577, in <module>
    main()
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 979, in main
    no_pip=options.no_pip)
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 1081, in create_environment
    site_packages=site_packages, clear=clear))
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 1289, in install_python
    writefile(site_filename_dst, SITE_PY)
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 445, in writefile
    f = open(dest, 'wb')
IOError: [Errno 2] No such file or directory: 'mayapy/lib/python26.zip/site.py'

I've read virtualenv's page, the output of virtualenv -h, a few basic virtualenv writeups like this one, and searched google and SO for both virtualenv and maya+virtualenv pairings, all to no avail.

Here's the verbose output:

$ virtualenv -vv -p /usr/autodesk/maya/bin/mayapy mayapy
Running virtualenv with interpreter /usr/autodesk/maya/bin/mayapy
PYTHONHOME is set.  You *must* activate the virtualenv before using it
Directory mayapy/lib/python2.6 already exists
Symlinking Python bootstrap modules
  Cannot import bootstrap module: os
  Ignoring built-in bootstrap module: posix
  Cannot import bootstrap module: posixpath
  Cannot import bootstrap module: nt
  Cannot import bootstrap module: ntpath
  Cannot import bootstrap module: genericpath
  Cannot import bootstrap module: fnmatch
  Cannot import bootstrap module: locale
  Cannot import bootstrap module: encodings
  Cannot import bootstrap module: codecs
  Cannot import bootstrap module: stat
  Cannot import bootstrap module: UserDict
  File mayapy/lib/python2.6/lib-dynload/readline.so already exists
  Cannot import bootstrap module: copy_reg
  Cannot import bootstrap module: types
  Cannot import bootstrap module: re
  Cannot import bootstrap module: sre
  Cannot import bootstrap module: sre_parse
  Cannot import bootstrap module: sre_constants
  Cannot import bootstrap module: sre_compile
  File mayapy/lib/python2.6/lib-dynload/zlib.so already exists
  Cannot import bootstrap module: warnings
  Cannot import bootstrap module: linecache
  Cannot import bootstrap module: _abcoll
  Cannot import bootstrap module: abc
Directory mayapy/lib/python2.6/site-packages already exists
Writing mayapy/lib/python26.zip/site.py
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 2577, in <module>
    main()
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 979, in main
    no_pip=options.no_pip)
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 1081, in create_environment
    site_packages=site_packages, clear=clear))
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 1289, in install_python
    writefile(site_filename_dst, SITE_PY)
  File "/usr/local/lib/python2.6/dist-packages/virtualenv.py", line 445, in writefile
    f = open(dest, 'wb')
IOError: [Errno 2] No such file or directory: 'mayapy/lib/python26.zip/site.py'
Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
  • I don't know the answer but seeing as comments have been thin... I create virtualenvs using the python I want to virtualize instead of using the -p option: `/usr/autodesk/maya/bin/mayapy virtualenv mayapy` – tdelaney May 21 '13 at 22:56
  • I'm starting to see that, though it took awhile to uncover. Does this mean I need to install virtualenv itself into Maya's python? – Gary Fixler May 21 '13 at 23:32
  • tried adding `/usr/local/lib/python2.6/dist-packages` or `/usr/autodesk/maya/bin/python26.zip` in the `PYTHONPATH` environment variable? – Bleeding Fingers May 22 '13 at 07:36

2 Answers2

0

Download the latest virtualenv and call it virtualenv with the version of python you want to virtualize:

/usr/autodesk/maya/bin/mayapy where/i/saved/virtualenv mayapy
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • `/usr/autodesk/maya/bin/mayapy /usr/local/bin/virtualenv mayapy` gives me `ImportError: No module named pkg_resources`. – Gary Fixler May 22 '13 at 03:15
  • @Gary Fixler - that means your python doesn't have [setuptools](https://pypi.python.org/pypi/setuptools). You can download and install it (using /usr/autodesk/maya/bin/mayapy) and at least get a step further. – tdelaney May 22 '13 at 15:04
  • Looks like it's not quite that easy. I'm on 64-bit Linux, so it wants me to either download and run an egg file like a shell script, which won't let me choose mayapy, or I can include EasyInstall command-line options, which of course I don't understsand. I'll have to figure out how to do it again on 64-bit Windows 7 at work next. – Gary Fixler May 22 '13 at 18:33
  • 1
    @Gary Fixler you might be able to sidestep the problems by downloading the (setuptools tarball)[https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz], unpacking with something like 7zip (or tar on linux), cd into the unpacked directory and then running `/usr/autodesk/maya/bin/mayapy setup.py`. – tdelaney May 22 '13 at 19:13
  • thanks for the heads up. It occurs to me that pretty much all of my problems are path-based. Paths are hard. – Gary Fixler May 24 '13 at 04:09
  • Thanks for the heads up on the standalone. I ended up using it with nose itself, along with `theodux`'s recommendation (`site.addsitedir()`) to allow me to point Maya at a folder of package folders. Now I can mod Maya without actually modding it, and without fighting virtualenv into this weird setup. – Gary Fixler Sep 07 '13 at 04:42
0

An alternative that's good for unittesting without messing with your maya runtime environment is to run your tests inside an instance of maya.standalone. If your tests all include something like this:

import maya.standalone
try: 
    maya.standalone.initialize()
except:
    pass # initialize raises if you call it more than once, but it's harmless if you catch it.

you can run them from the mayapy intepreter without running maya in gui mode.

theodox
  • 12,028
  • 3
  • 23
  • 36
  • That's what I'm using. It's been [an ongoing saga](http://stackoverflow.com/questions/12087780/is-there-any-way-to-install-nose-in-maya). You can see in one of my comments [here](http://stackoverflow.com/questions/12087780/is-there-any-way-to-install-nose-in-maya/15213747#15213747) that I figured out how to easy_install in Maya, and I actually got nose working. I received a comment to that that made me reconsider screwing with Maya's environment, though, so I wanted to see if virtualenv would let me wrap mayapy so I could just switch my global environment to it temporarily to run tests. – Gary Fixler May 24 '13 at 04:05
  • 1
    FWIW , I've had great luck keeping all my maya code in a single zip file and adding it to the path using site.addsitedir(path/to/zip). I don't install anything into the maya directory that way, and I can swap toolsets just by changing a line in userSetup.py or setting an envrionment variable. It's also nice for testing because there no chance of old pyc files with bad code lying around at test time ;) – theodox May 24 '13 at 21:22
  • I wanted to come back and say thanks. I had occasion to reinstall Maya recently (system crash at work), and was looking through this question again to see how I set up nose last time. I noticed the zip idea and gave it a whirl. I haven't gotten a zip file itself to work (tried .zip, .tar.gz, full paths, folder path, etc), but adding a `site.addsitedir('path/to/nose-1.3.0')` to my userSetup.py finds the uncompressed directory just fine. This is a decent setup. Thanks for recommending it earlier this year. – Gary Fixler Sep 07 '13 at 04:39