23

Can I download and install Python modules from PyPi strictly inside a script, without using a shell at all?

I use a non-standard Python environment, Autodesk Maya 2012's Python interpreter. This does not come with easy_install or pip, and there is no shell, only a python script interpreter invoked by the main Maya executable. Copying and pasting ez_setup.py's contents into the script editor window and running it correctly installs an easy_install somewhere into Maya's directory, but the script incorrectly records the Python interpreter as ...maya.exe instead of ...mayapy.exe Furthermore, using easy_install requires a shell.

The objective is to deliver a Python script that, e.g., installs NumPy into the Maya Python system. This could be accomplished by dropping eggs into the site-packages directory, but that requires manual user intervention. Anything an end user has to do outside the Maya environment is essentially untouchable, especially messing with the file system. But messing with the filesystem through a script? That's fine.

Is there something more elegant than ez_setup.py + editing the resulting easy_install...py's + subprocess calls? I feel like this is a basic feature. I see documentation online for programmatic module installation through pip, but pip needs to be installed first!

What is the most elegant way to install a module strictly within the confines of a script?

3
  • Does assigning sys.executable to the path to mayapy.exe before running ez_setup.py work? Commented Oct 19, 2012 at 1:36
  • Thanks: this fixes ez_setup, but it requires knowing where mayapy.exe is. More path hacking... Commented Oct 19, 2012 at 21:05
  • 1
    If you don't need easy_install's dependency resolution, simply exec()-ing the setup.py file might work Commented Oct 22, 2012 at 18:41

3 Answers 3

18

Installing easy_install for Maya on windows.

  1. Download ez_setup.py.
  2. open windows cmd elevated (start, type cmd, rmb click on it ->run as administrator)
  3. change the cmd directory to x:\maya install dir\bin
    • example: cd c:\Program Files\MayaXX\bin
  4. execute following command mayapy x:\WhereYouSaved\ez_setup.py

Now easy install should be set up properly. You may want to still do following steps:

  1. cd x:\maya install dir\python\scripts
  2. rename all files in this folder to start with ma
    • example: for %i in (*) do ren %i ma%i
  3. add this folder to your path
    • hit win+e
    • rmb my computer and choose properties
    • Advanced system settings -> Environment variables
    • search variable path edit it and append ;x:\maya install dir\python\scripts

Now you can call maeasy_install pythonModule from cmd for installing stuff. Also you can call following inside Maya to install modules:

from setuptools.command import easy_install
easy_install.main( ["pythonModule"] )

NOTE: If Maya is installed in program files then you can not really install stuff without elevating. Unless you change disk permissions to the Maya python directory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. easy_install.main() is exactly what I'm looking for, because executing a modified ez_setup.py inside Maya gets it installed too.
11
#!/usr/bin/env python

from __future__ import print_function

REQUIREMENTS = [ 'distribute', 'version', 'Cython', 'sortedcollection' ]
try:
    from setuptools import find_packages
    from distutils.core import setup
    from Cython.Distutils import build_ext as cython_build
    import sortedcollection
except:
    import os, pip
    pip_args = [ '-vvv' ]
    proxy = os.environ['http_proxy']
    if proxy:
        pip_args.append('--proxy')
        pip_args.append(proxy)
    pip_args.append('install')
    for req in REQUIREMENTS:
        pip_args.append( req )
    print('Installing requirements: ' + str(REQUIREMENTS))
    pip.main(initial_args = pip_args)

    # do it again
    from setuptools import find_packages
    from distutils.core import setup
    from Cython.Distutils import build_ext as cython_build
    import sortedcollection

2 Comments

This looks really interesting. Does this try importing sortedcollection, and if it fails to import, install the package?
@DoctorPangloss: Yes, correct. It tries to import Cython and sortedcollection inside the try/catch block. When it fails, it uses pip to install dependencies listed in REQUIREMENTS. Then it tries to import cython and sortedcollections again, which should work this time.
1

To make it work, open the ez_setup.py file and simply add an s after http at this line:

DEFAULT_URL     = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]

so that it becomes

DEFAULT_URL     = "https://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.