How can I install Python modules programmatically / through a Python script? [duplicate]

Solution 1:

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.

Solution 2:

#!/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

Solution 3:

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]