Installing modules via setuptools in a script

Discussion in 'Python' started by Thorsten Kampe, Nov 24, 2007.

  1. Date: Fri, 23 Nov 2007 19:35:21 -0000
    Message-ID: <>
    MIME-Version: 1.0
    Content-Type: text/plain; charset="utf-8"
    Content-Transfer-Encoding: 7bit
    User-Agent: MicroPlanet-Gravity/2.70.2067

    Hi,

    can anyone give me a short code snippet how to install a missing
    module via setuptools (assuming setuptools is already installed)?!

    Something like this:

    try:
    import missing_module
    except import_error
    import setuptools
    setuptools.whatever.install(missing_module)


    Thorsten
     
    Thorsten Kampe, Nov 24, 2007
    #1
    1. Advertising

  2. Thorsten Kampe

    Robert Kern Guest

    Thorsten Kampe wrote:

    > Hi,
    >
    > can anyone give me a short code snippet how to install a missing
    > module via setuptools (assuming setuptools is already installed)?!
    >
    > Something like this:
    >
    > try:
    > import missing_module
    > except import_error
    > import setuptools
    > setuptools.whatever.install(missing_module)


    The recommended way to handle dependencies using setuptools is to specify them
    in the install_requires metadata in the setup() function call in your setup.py:

    # http://peak.telecommunity.com/DevCenter/setuptools#basic-use
    setup(name="foo",
    ...
    install_requires = [
    'some_package >= 1.0',
    'another_package',
    ],
    )

    However, if you have special needs that really do require downloading the
    dependency at runtime instead of install-time:

    #
    http://peak.telecommunity.com/DevCenter/PkgResources#workingset-methods-and-attributes

    import pkg_resources
    pkg_resources.resolve('some_package >= 1.0')
    pkg_resources.resolve('another_package')

    import some_package
    import another_package

    But, please be sure that that your needs are special.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Nov 24, 2007
    #2
    1. Advertising

  3. * Robert Kern (Sat, 24 Nov 2007 16:33:37 -0600)
    > Thorsten Kampe wrote:
    > > can anyone give me a short code snippet how to install a missing
    > > module via setuptools (assuming setuptools is already installed)?!
    > >
    > > Something like this:
    > >
    > > try:
    > > import missing_module
    > > except import_error
    > > import setuptools
    > > setuptools.whatever.install(missing_module)

    >
    > The recommended way to handle dependencies using setuptools is to specify them
    > in the install_requires metadata in the setup() function call in your setup.py:


    It's just a simple script - no package. So I don't even have a
    setup.py.

    > However, if you have special needs that really do require downloading the
    > dependency at runtime instead of install-time:
    >
    > #
    > http://peak.telecommunity.com/DevCenter/PkgResources#workingset-methods-and-attributes
    >
    > import pkg_resources
    > pkg_resources.resolve('some_package >= 1.0')
    > pkg_resources.resolve('another_package')
    >
    > import some_package
    > import another_package


    [5]>>> pkg_resources.working_set.resolve('betterprint')
    ----------------------------------------------------------------------
    -----
    AttributeError Traceback (most recent call
    last)

    F:\program files\python\<ipython console> in <module>()

    F:\program files\python\lib\site-packages\setuptools-0.6c5-py2.5.egg
    \pkg_resou
    rces.py in resolve(self=<pkg_resources.WorkingSet object at
    0x01457710>, requi
    rements=['n', 'i', 'r', 'p', 'r', 'e', 't', 't', 'e', 'b'], env=None,
    installe
    r=None)
    472 # Ignore cyclic or redundant dependencies
    473 continue
    --> 474 dist = best.get(req.key)
    dist = undefined
    best.get = <built-in method get of dict object at 0x016BC660>
    req.key = undefined
    475 if dist is None:
    476 # Find the best distribution and add it to the
    map

    AttributeError: 'str' object has no attribute 'key'
     
    Thorsten Kampe, Nov 25, 2007
    #3
  4. Thorsten Kampe

    Ben Finney Guest

    Thorsten Kampe <> writes:

    > * Robert Kern (Sat, 24 Nov 2007 16:33:37 -0600)
    > > Thorsten Kampe wrote:
    > > > can anyone give me a short code snippet how to install a missing
    > > > module via setuptools (assuming setuptools is already
    > > > installed)?!

    > >
    > > The recommended way to handle dependencies using setuptools is to
    > > specify them in the install_requires metadata in the setup()
    > > function call in your setup.py:

    >
    > It's just a simple script - no package. So I don't even have a
    > setup.py.


    The recommended way of installing a simple script that has
    dependencies is to write a setup.py for the simple script, so that you
    can declare its dependencies and have them checked on install.

    --
    \ "Pinky, are you pondering what I'm pondering?" "Well, I think |
    `\ so, Brain, but pantyhose are so uncomfortable in the |
    _o__) summertime." -- _Pinky and The Brain_ |
    Ben Finney
     
    Ben Finney, Nov 25, 2007
    #4
  5. * Ben Finney (Mon, 26 Nov 2007 09:04:51 +1100)
    > Thorsten Kampe <> writes:
    > > * Robert Kern (Sat, 24 Nov 2007 16:33:37 -0600)
    > > > Thorsten Kampe wrote:
    > > > > can anyone give me a short code snippet how to install a missing
    > > > > module via setuptools (assuming setuptools is already
    > > > > installed)?!
    > > >
    > > > The recommended way to handle dependencies using setuptools is to
    > > > specify them in the install_requires metadata in the setup()
    > > > function call in your setup.py:

    > >
    > > It's just a simple script - no package. So I don't even have a
    > > setup.py.

    >
    > The recommended way of installing a simple script that has
    > dependencies is to write a setup.py for the simple script, so that you
    > can declare its dependencies and have them checked on install.


    Yes, I know. But this script is not going to be installed - just run.
    And therefore I'd like to know if it's possible to install missing
    dependencies in the script itself via importing setuptools. Or do I
    have to use "subprocess('easy_install')"?

    Thorsten
     
    Thorsten Kampe, Nov 26, 2007
    #5
  6. Thorsten Kampe

    Robert Kern Guest

    Thorsten Kampe wrote:
    > * Robert Kern (Sat, 24 Nov 2007 16:33:37 -0600)
    >> Thorsten Kampe wrote:
    >>> can anyone give me a short code snippet how to install a missing
    >>> module via setuptools (assuming setuptools is already installed)?!
    >>>
    >>> Something like this:
    >>>
    >>> try:
    >>> import missing_module
    >>> except import_error
    >>> import setuptools
    >>> setuptools.whatever.install(missing_module)

    >> The recommended way to handle dependencies using setuptools is to specify them
    >> in the install_requires metadata in the setup() function call in your setup.py:

    >
    > It's just a simple script - no package. So I don't even have a
    > setup.py.
    >
    >> However, if you have special needs that really do require downloading the
    >> dependency at runtime instead of install-time:
    >>
    >> #
    >> http://peak.telecommunity.com/DevCenter/PkgResources#workingset-methods-and-attributes
    >>
    >> import pkg_resources
    >> pkg_resources.resolve('some_package >= 1.0')
    >> pkg_resources.resolve('another_package')
    >>
    >> import some_package
    >> import another_package

    >
    > [5]>>> pkg_resources.working_set.resolve('betterprint')
    > ----------------------------------------------------------------------
    > -----
    > AttributeError Traceback (most recent call
    > last)
    >
    > F:\program files\python\<ipython console> in <module>()
    >
    > F:\program files\python\lib\site-packages\setuptools-0.6c5-py2.5.egg
    > \pkg_resou
    > rces.py in resolve(self=<pkg_resources.WorkingSet object at
    > 0x01457710>, requi
    > rements=['n', 'i', 'r', 'p', 'r', 'e', 't', 't', 'e', 'b'], env=None,
    > installe
    > r=None)
    > 472 # Ignore cyclic or redundant dependencies
    > 473 continue
    > --> 474 dist = best.get(req.key)
    > dist = undefined
    > best.get = <built-in method get of dict object at 0x016BC660>
    > req.key = undefined
    > 475 if dist is None:
    > 476 # Find the best distribution and add it to the
    > map
    >
    > AttributeError: 'str' object has no attribute 'key'


    My apologies for misleading you. There is no easy way to do this. Here is a
    roundabout way which might be suitable for a throwaway hack script. If it's not
    a throwaway hack script, then please heed Ben's advice. Alternatively, just
    distribute betterprint along with your script and save yourself the headache.


    In [1]: import betterprint
    ---------------------------------------------------------------------------
    ImportError Traceback (most recent call last)

    /Users/rkern/<ipython console> in <module>()

    ImportError: No module named betterprint

    In [2]: import pkg_resources

    In [3]: from setuptools.dist import Distribution

    In [4]:
    pkg_resources.working_set.resolve(pkg_resources.parse_requirements('betterprint'),
    installer=Distribution().fetch_build_egg)
    zip_safe flag not set; analyzing archive contents...

    Installed /Users/rkern/betterprint-0.1-py2.5.egg
    Out[4]: [betterprint 0.1 (/Users/rkern/betterprint-0.1-py2.5.egg)]

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Nov 26, 2007
    #6
  7. * Robert Kern (Mon, 26 Nov 2007 04:34:17 -0600)
    > Thorsten Kampe wrote:
    > > * Robert Kern (Sat, 24 Nov 2007 16:33:37 -0600)
    > >> Thorsten Kampe wrote:
    > >>> can anyone give me a short code snippet how to install a missing
    > >>> module via setuptools (assuming setuptools is already installed)?!
    > >>>
    > >>> Something like this:
    > >>>
    > >>> try:
    > >>> import missing_module
    > >>> except import_error
    > >>> import setuptools
    > >>> setuptools.whatever.install(missing_module)
    > >> The recommended way to handle dependencies using setuptools is to specify them
    > >> in the install_requires metadata in the setup() function call in your setup.py:

    > >
    > > It's just a simple script - no package. So I don't even have a
    > > setup.py.

    [...]
    > My apologies for misleading you. There is no easy way to do this. Here is a
    > roundabout way which might be suitable for a throwaway hack script. If it's not
    > a throwaway hack script, then please heed Ben's advice. Alternatively, just
    > distribute betterprint along with your script and save yourself the headache.
    >
    >
    > In [1]: import betterprint
    > ---------------------------------------------------------------------------
    > ImportError Traceback (most recent call last)
    >
    > /Users/rkern/<ipython console> in <module>()
    >
    > ImportError: No module named betterprint
    >
    > In [2]: import pkg_resources
    >
    > In [3]: from setuptools.dist import Distribution
    >
    > In [4]:
    > pkg_resources.working_set.resolve(pkg_resources.parse_requirements('betterprint'),
    > installer=Distribution().fetch_build_egg)
    > zip_safe flag not set; analyzing archive contents...
    >
    > Installed /Users/rkern/betterprint-0.1-py2.5.egg
    > Out[4]: [betterprint 0.1 (/Users/rkern/betterprint-0.1-py2.5.egg)]


    Okay, works for me, thanks. Is there an option to have the downloaded
    module installed into the "site-packages" directory (and not into the
    current)?

    Thorsten
     
    Thorsten Kampe, Nov 26, 2007
    #7
  8. Thorsten Kampe

    Robert Kern Guest

    Thorsten Kampe wrote:
    > * Robert Kern (Mon, 26 Nov 2007 04:34:17 -0600)
    >> Thorsten Kampe wrote:
    >>> * Robert Kern (Sat, 24 Nov 2007 16:33:37 -0600)
    >>>> Thorsten Kampe wrote:
    >>>>> can anyone give me a short code snippet how to install a missing
    >>>>> module via setuptools (assuming setuptools is already installed)?!
    >>>>>
    >>>>> Something like this:
    >>>>>
    >>>>> try:
    >>>>> import missing_module
    >>>>> except import_error
    >>>>> import setuptools
    >>>>> setuptools.whatever.install(missing_module)
    >>>> The recommended way to handle dependencies using setuptools is to specify them
    >>>> in the install_requires metadata in the setup() function call in your setup.py:
    >>> It's just a simple script - no package. So I don't even have a
    >>> setup.py.

    > [...]
    >> My apologies for misleading you. There is no easy way to do this. Here is a
    >> roundabout way which might be suitable for a throwaway hack script. If it's not
    >> a throwaway hack script, then please heed Ben's advice. Alternatively, just
    >> distribute betterprint along with your script and save yourself the headache.
    >>
    >>
    >> In [1]: import betterprint
    >> ---------------------------------------------------------------------------
    >> ImportError Traceback (most recent call last)
    >>
    >> /Users/rkern/<ipython console> in <module>()
    >>
    >> ImportError: No module named betterprint
    >>
    >> In [2]: import pkg_resources
    >>
    >> In [3]: from setuptools.dist import Distribution
    >>
    >> In [4]:
    >> pkg_resources.working_set.resolve(pkg_resources.parse_requirements('betterprint'),
    >> installer=Distribution().fetch_build_egg)
    >> zip_safe flag not set; analyzing archive contents...
    >>
    >> Installed /Users/rkern/betterprint-0.1-py2.5.egg
    >> Out[4]: [betterprint 0.1 (/Users/rkern/betterprint-0.1-py2.5.egg)]

    >
    > Okay, works for me, thanks. Is there an option to have the downloaded
    > module installed into the "site-packages" directory (and not into the
    > current)?


    No. This is a hack. If you need things installed properly, use a setup.py.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Nov 26, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. limodou
    Replies:
    0
    Views:
    275
    limodou
    Aug 5, 2005
  2. yoda
    Replies:
    5
    Views:
    4,677
  3. Kless
    Replies:
    0
    Views:
    225
    Kless
    Dec 19, 2008
  4. Eric Frederich
    Replies:
    0
    Views:
    372
    Eric Frederich
    Apr 18, 2011
  5. Oktaka Com
    Replies:
    1
    Views:
    215
    Cameron Simpson
    Jan 7, 2012
Loading...

Share This Page