Ron D.
Ron D.

Reputation: 3814

"import site' failed; use -v for traceback" - mac os lion, python 2.7, subprocess

I'm trying to run a subprocess in python, this a part from my code:

def update(self):
        currentTime = strftime("%d.%m.%y %H:%M", gmtime()) #strftime("%d-%m-%y %H:%M", gmtime)
        resultString = "======== " + currentTime + " ========\n\n"  
        bzrMergeCommand = "cd %s ; /usr/local/bin/bzr merge" % self._directoryName
        print "Getting the updated code from bzr..."
        mergeResult = sp.Popen(bzrMergeCommand, shell=True, stdout=sp.PIPE, stderr=sp.PIPE, cwd= self._directoryName)

        communicated = mergeResult.communicate()

But it fail to run and this is the exception I got:

 'import site' failed; use -v for traceback Traceback (most recent call
 last):   File "/usr/local/bin/bzr", line 21, in <module>
     import os   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py",
 line 398, in <module>
     import UserDict   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py",
 line 84, in <module>
     _abcoll.MutableMapping.register(IterableUserDict)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py",
 line 109, in register
     if issubclass(subclass, cls):   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py",
 line 151, in __subclasscheck__
     if subclass in cls._abc_cache:   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py",
 line 69, in __contains__
     return ref(item) in self.data TypeError: cannot create weak reference to 'classobj' object

I googled and read alot regarding "TypeError: cannot create weak reference to 'classobj' object": https://stackoverflow.com/questions/7753181/making-my-python-script-executable-causes-a-import-site-failed-use-v-for-tra

and here: https://github.com/pypa/virtualenv/issues/108

Any idea?

Upvotes: 2

Views: 2076

Answers (3)

Simon Whitaker
Simon Whitaker

Reputation: 20586

As others have noted, the problem is with Bazaar itself (/usr/local/bin/bzr), not your script.

According to the Bazaar website, you need to tweak Bazaar to use Python 2.6 on OS X Lion:

Note: to use Bazaar in OS X Lion (10.7), you should change the version of Python used by the 'bzr' script to 2.6. You can do this with one command in a terminal:

sudo sed -i '' s,/usr/bin/python,/usr/bin/python2.6, /usr/local/bin/bzr

Upvotes: 0

Perkins
Perkins

Reputation: 2517

The error raised is from bzr, not your script. Try running python and typing import site If that fails, you might have something broken with your install of python.

Also, as a general rule, unless you have a reason to set shell=True in Popen, it is better to set shell=False.

Upvotes: 1

Rob Wouters
Rob Wouters

Reputation: 16327

It looks like the problem is in /usr/local/bin/bzr and not in your script. Try running your bzrMergeCommand from the commandline, i.e. without using a script. You should get the same error.

Try editing the shebang in /usr/local/bin/bzr as suggested in your linked StackOverflow question.

Upvotes: 0

Related Questions