0

I would like to download and install setup_tools, easy_install and tinydb all from within mayas python interpreter..

check out setup() in the code below.

I'm pretty close, but it looks like the system command to run ez-setup.py is not downloading the easy_install packages to mayas site_packages directory, which is strange because the same command works perfectly in the shell..

So the system call reads like this: /Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy /Users/paxtongerrish/downloads/ez_setup.py

I am pointing mayas python interpreter at ez_setup.py

when i punch this command into the shell, it downloads setup_tools to mayas python site_packages directory... Great! :D

However.. I need to have this all happen from inside mayas python interpreter and it does not work when called from os.system or subprocess.call

thanks! import os import sys import urllib2 import subprocess

setup_tools_address = 'https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py'
downloads_directory = '%s/downloads' % os.getenv('HOME')

if not os.path.exists(downloads_directory):
    os.makedirs(downloads_directory)

def setup():
    url = setup_tools_address
    file_path = '%s/%s' % (downloads_directory, url.split('/')[-1])
    maya_py = maya_py_path()
    for p in download_url(url, file_path):
        print p
    system_command = '%s %s' % (maya_py, file_path)
    print '----- sys command---  (only works in shell)------\n'
    print system_command
    print '\n----------------------------------------------\n'
    #This system command works from shell, but not from python.... Maybe superuser thing??
    #os.system(system_command)
    #sub process doesnt work either

    p = subprocess.Popen([maya_py, file_path], shell=True, stdout=subprocess.PIPE)
    print '--->>'
    p.wait()
    for i in p.stdout.readline():
        sys.stdout.flush()
        print i
    add_eggs()
    from setuptools.command import easy_install
    easy_install.main(['tinydb'])
    add_eggs()
    import tinydb

def download_url(url, file_path, block_size=2056):
    request = urllib2.urlopen(url)
    file_size = int(request.info()['Content-Length'])
    if not file_path:
        file_path = '%s/%s' % (os.getenv('HOME'), url.split("/")[-1])
    downloaded_chunk = 0
    with open(file_path, "wb") as f:
        while downloaded_chunk < file_size:
            chunk = request.read(block_size)
            downloaded_chunk += len(chunk)
            f.write(chunk)
            progress = float(downloaded_chunk) / file_size * 100
            yield progress
        print("\nDownload Complete.")

def maya_app_path():
    appName = 'Maya'
    if sys.platform == 'win32':
        appName = 'Maya.exe'
    for p in sys.path:
        app_path = '%s/%s' % (p.replace('\\','/') , appName)
        if os.path.exists(app_path):
            return app_path

def maya_py_path():
    file_name = 'mayapy'
    if sys.platform == 'win32':
        file_name = 'mayapy.exe'
        return '%s\\%s' % (os.path.dirname(maya_app_path().replace('/','\\')), file_name.replace('/','\\'))
    return '%s/bin/%s' % (os.path.dirname(os.path.dirname(maya_app_path())), file_name)

def get_site_packages_directory():
    for p in sys.path:
        if p.endswith('site-packages'):
            return p

def add_eggs():
    site_packages_directory = get_site_packages_directory()
    for item in os.listdir(site_packages_directory):
        if item.endswith('.egg'):
            sys.path.append('%s/%s' % (site_packages_directory, item))
Pax
  • 237
  • 3
  • 11
  • When you say not working, what do you mean? Is there an exception? Does the command line fail? Try using subprocess and setting `stdout=PIPE` so you can get the output from the command. – Brendan Abel Jan 20 '16 at 20:06
  • Hey, I tried to clarify this by editing the original post.. – Pax Jan 20 '16 at 20:15
  • Again, what happens exactly? What is the output when running the shell command from python (you'll need to use `subprocess.Popen()` since `subprocess.call` may block if you try to write the stderr or stdout to a PIPE. – Brendan Abel Jan 20 '16 at 20:15
  • I tried subprocess.call([maya_py, file_path], stdout=subprocess.PIPE) same issue. i will try Popen – Pax Jan 20 '16 at 20:20
  • Do this: `p = Popen(cmd, stdout=PIPE); p.wait(); print p.stdout.read()` – Brendan Abel Jan 20 '16 at 20:26
  • Generally speaking it's very hard to use the standard pip/easy-install route for Maya if you're planning on sharing this with anybody else: there are permissions issues and access rights (in many studios, artists aren't admins on their own machines and can't run with root privileges or access the Maya install directory). Lots of resources on this question [here](http://tech-artists.org/forum/showthread.php?5309-Deploy-tools-for-your-Maya-team&highlight=distribution) – theodox Jan 27 '16 at 21:19
  • Have you read [this](http://stackoverflow.com/questions/12966147/how-can-i-install-python-modules-programmatically-through-a-python-script/13016849#13016849)? – joojaa Oct 23 '16 at 09:00

0 Answers0