views:

284

answers:

4

Hello.

easy_install python extension allows to install python eggs from console like:

easy_install py2app

But is it possible to access easy_install functionality inside a python script? I means, without calling os.system( "easy_install py2app" ) but instead importing easy_install as a python module and using it's native methods?

A: 

I think you can get to that by using either importing setuptools.

notzach
And what methods to call or where to look for documentation? :)
Eye of Hell
+3  A: 

When I look at the setup tools source, it looks like you can try the following.

from setuptools.command import easy_install
easy_install.main( ["-U","py2app"] )
S.Lott
Will not work, easy_install.easy_install() will rise TypeError: 'dist must be a Distribution instance'
Eye of Hell
Found on google search: easy_install.main( "-U py2app".split() ). Please change your answer text so i can accept it :)
Eye of Hell
I can confirm that this works - I do it all the time to build custom-install scripts for my python project. +1
Salim Fadhley
+1  A: 

What specifically are you trying to do? Unless you have some weird requirements, I'd recommend declaring the package as a dependency in your setup.py:

from setuptools import setup, find_packages
setup(
    name = "HelloWorld",
    version = "0.1",
    packages = find_packages(),
    scripts = ['say_hello.py'],

    # Project uses reStructuredText, so ensure that the docutils get
    # installed or upgraded on the target machine
    install_requires = ['docutils>=0.3'],

    package_data = {
        # If any package contains *.txt or *.rst files, include them:
        '': ['*.txt', '*.rst'],
        # And include any *.msg files found in the 'hello' package, too:
        'hello': ['*.msg'],
    }

    # metadata for upload to PyPI
    author = "Me",
    author_email = "[email protected]",
    description = "This is an Example Package",
    license = "PSF",
    keywords = "hello world example examples",
    url = "http://example.com/HelloWorld/",   # project home page, if any

    # could also include long_description, download_url, classifiers, etc.
)

The key line here is install_requires = ['docutils>=0.3']. This will cause the setup.py file to automatically install this dependency unless the user specifies otherwise. You can find more documentation on this here (note that the setuptools website is extremely slow!).

If you do have some kind of requirement that can't be satisfied this way, you should probably look at S.Lott's answer (although I've never tried that myself).

Jason Baker
+2  A: 
from setuptools.command import easy_install

def install_with_easyinstall(package):
    easy_install.main(["-U", package]).

install_with_easyinstall('py2app')
nosklo
+1 - this works! :-)
Salim Fadhley