Using easy_install inside a python script?

狂风中的少年 提交于 2019-12-04 10:12:27

问题


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?


回答1:


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"] )



回答2:


from setuptools.command import easy_install

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

install_with_easyinstall('py2app')



回答3:


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 = "me@example.com",
    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).




回答4:


The answer about invoking setuptools.main() is correct. However, if setuptools creates a .egg, the script will be unable to import the module after installing it. Eggs are added automatically to sys.path at python start time.

One solution is to use require() to add the new egg to the path:

from setuptools.command import easy_install
import pkg_resources
easy_install.main( ['mymodule'] )
pkg_resources.require('mymodule')



回答5:


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



来源:https://stackoverflow.com/questions/935111/using-easy-install-inside-a-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!