473,785 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Installing modules via setuptools in a script

Date: Fri, 23 Nov 2007 19:35:21 -0000
Message-ID: <MP************ ************@ne ws.individual.d e>
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.what ever.install(mi ssing_module)
Thorsten
Nov 24 '07 #1
7 2315
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.what ever.install(mi ssing_module)
The recommended way to handle dependencies using setuptools is to specify them
in the install_require s metadata in the setup() function call in your setup.py:

# http://peak.telecommunity.com/DevCen...ools#basic-use
setup(name="foo ",
...
install_require s = [
'some_package >= 1.0',
'another_packag e',
],
)

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

#
http://peak.telecommunity.com/DevCen...and-attributes

import pkg_resources
pkg_resources.r esolve('some_pa ckage >= 1.0')
pkg_resources.r esolve('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

Nov 24 '07 #2
* 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.what ever.install(mi ssing_module)

The recommended way to handle dependencies using setuptools is to specify them
in the install_require s 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/DevCen...and-attributes

import pkg_resources
pkg_resources.r esolve('some_pa ckage >= 1.0')
pkg_resources.r esolve('another _package')

import some_package
import another_package
[5]>>pkg_resources .working_set.re solve('betterpr int')
----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)

F:\program files\python\<i python consolein <module>()

F:\program files\python\li b\site-packages\setupt ools-0.6c5-py2.5.egg
\pkg_resou
rces.py in resolve(self=<p kg_resources.Wo rkingSet 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.ke y)
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'
Nov 25 '07 #3
Thorsten Kampe <th******@thors tenkampe.dewrit es:
* 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_require s 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
Nov 25 '07 #4
* Ben Finney (Mon, 26 Nov 2007 09:04:51 +1100)
Thorsten Kampe <th******@thors tenkampe.dewrit es:
* 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_require s 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('ea sy_install')"?

Thorsten
Nov 26 '07 #5
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.what ever.install(mi ssing_module)
The recommended way to handle dependencies using setuptools is to specify them
in the install_require s 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/DevCen...and-attributes

import pkg_resources
pkg_resources.r esolve('some_pa ckage >= 1.0')
pkg_resources.r esolve('another _package')

import some_package
import another_package

[5]>>pkg_resources .working_set.re solve('betterpr int')
----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)

F:\program files\python\<i python consolein <module>()

F:\program files\python\li b\site-packages\setupt ools-0.6c5-py2.5.egg
\pkg_resou
rces.py in resolve(self=<p kg_resources.Wo rkingSet 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.ke y)
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 consolein <module>()

ImportError: No module named betterprint

In [2]: import pkg_resources

In [3]: from setuptools.dist import Distribution

In [4]:
pkg_resources.w orking_set.reso lve(pkg_resourc es.parse_requir ements('betterp rint'),
installer=Distr ibution().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

Nov 26 '07 #6
* 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.what ever.install(mi ssing_module)
The recommended way to handle dependencies using setuptools is to specify them
in the install_require s 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 consolein <module>()

ImportError: No module named betterprint

In [2]: import pkg_resources

In [3]: from setuptools.dist import Distribution

In [4]:
pkg_resources.w orking_set.reso lve(pkg_resourc es.parse_requir ements('betterp rint'),
installer=Distr ibution().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
Nov 26 '07 #7
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.what ever.install(mi ssing_module)
The recommended way to handle dependencies using setuptools is to specify them
in the install_require s 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 consolein <module>()

ImportError: No module named betterprint

In [2]: import pkg_resources

In [3]: from setuptools.dist import Distribution

In [4]:
pkg_resources. working_set.res olve(pkg_resour ces.parse_requi rements('better print'),
installer=Dist ribution().fetc h_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

Nov 26 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3212
by: Edward Diener | last post by:
I can install Python 2.4 on the Fedora 3 Linux system, but after I do a number of Linux utilities and commands, like yum, stop working because they were dependent on the Python 2.3 installation. What happens is that Python 2.4 replaces the /usr/bin/python module with the Python 2.4 version. If I replace /usr/bin/python with the Python 2.3 version executable, which is still on my system, that all the aforesaid modules depend on, they start...
0
1070
by: limodou | last post by:
I'm new to setuptools. One question is: Recently I want to use setuptools for a project. My command line is just like : python setup.py sdist --formats=gztar But I found some deleted files also included in the package. These files are .pyc suffixed. I use subversion. I checked the setuptools'
1
1435
by: Ilias Lazaridis | last post by:
If I understood things right, setuptools extends the functionality of distutils Thus replacing within a setup.py: from distutils.core import setup with try:
2
1063
by: Chaz Ginger | last post by:
Here is a problem I am trying to solve; I am sure there must be an easy way to do it and I just don't know how. I have a rather large application that I am writing. To make it easy for the user to run I have them run a startup.py script. This script will try to load each of the third party libraries the application will need. If it is present, great. If it isn't, I would like to automatically install it. This is the heart of my problem:...
0
3437
by: TiNo | last post by:
Hi, I'm having problems installing easy_install. When I run python ez_setup.py I get: G:\python>python ez_setup.py 'import site' failed; use -v for traceback Downloading http://cheeseshop.python.org/packages/2.5/s/setuptools/setuptools-0. 6c3-py2.5.egg Processing setuptools-0.6c3-py2.5.egg
11
4023
by: Tina I | last post by:
Hi list, Is there a preferred way to distribute programs that depends on third party modules like PyQt, Beautifulsoup etc? I have used setuptools and just having the setup script check for the existence of the required modules. If they're not found I have it exit with a message that it need this or that installed. But this is not very convenient for the end user and I have got a few complaints about it. Am I missing something in...
5
3776
by: Larry Bates | last post by:
Info: Python version: ActivePython 2.5.1.1 Platform: Windows I wanted to install BeautifulSoup today for a small project and decided to use easy_install. I can install other packages just fine. Unfortunately I get the following error from BeautifulSoup installation attempt: C:\Python25\Lib\SITE-P~1>easy_install BeautifulSoup
0
1115
by: Gabriel Genellina | last post by:
En Thu, 10 Jul 2008 14:02:29 -0300, Bhagwat Kolde <bbkolde@gmail.com> escribi�: If it is a single Python module, you can put it anywhere in the Python path. A good place may be the site-packages subdirectory. Execute this line to see the list of directories that are currently searched for modules: python -c "import sys; print '\n'.join(sys.path)"
0
2158
by: trihaitran | last post by:
I am reading chapter two of the Pylons book. In the text I am told to run: $ python virtualenv.py --no-site-packages env This is what I got back: $ python virtualenv.py --no-site-packages env New python executable in env/bin/python Installing setuptools........... Complete output from command env/bin/python -c "#!python
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.