Solved

Using easy install from python

Posted on 2011-03-04
1
374 Views
Last Modified: 2012-05-11
hi,

I have the following code in python
cmd = "easy_install boto"
os.system(cmd)
import boto

It installed boto but cant find boto unless I restart.

Anyway around this without having to restart?  What is the best way to do this?
0
Comment
Question by:dmontgom
1 Comment
 
LVL 25

Accepted Solution

by:
clockwatcher earned 125 total points
ID: 35042968
It can't find boto because python's import paths are built at start up.   To import the newly installed module within the running interpreter, explicitly add the new module's path to one of python's import paths-- sys.path, for example.  

Try the following:  
import sys
import os
import os.path
import distutils.sysconfig 

# install the module
from setuptools.command import easy_install
easy_install.main( ["boto"] );

# rebuild the list of available site-packages
sitedir = distutils.sysconfig.get_python_lib()
sitepackages = [sitedir + os.path.sep + i for i in os.listdir(sitedir)]

# current locations our python knows about 
paths = {}
for path in sys.path:
    paths[path]=""

# add any new locations that we don't know about
for fname in sitepackages:
    if fname in paths:
        continue 
    if os.path.isdir(fname):
        print "Adding path '%s' to sys.path" % fname
        sys.path.insert(1, fname)

import boto
s3 = boto.connect_s3()

Open in new window


Not exactly sure why you'd want to do this though.  Using setuptools or pip to put together a deployment package with dependencies seems like it might be a much better option.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question