2

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?

Community
  • 1
  • 1
Ron D.
  • 3,774
  • 6
  • 31
  • 39

3 Answers3

1

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.

Perkins
  • 2,409
  • 25
  • 23
  • shell=False gives: raise child_exception OSError: [Errno 2] No such file or directory Also, I ran it from command line and it was ok. – Ron D. Jan 05 '12 at 12:59
  • @ron: That is because if shell=True, it starts your default shell as the subprocess and passes the string bzrMergeCommand as the argument to it. If you set shell=False, then it looks for a file to execute called the value of bzrMergeCommand (not the program, which is the first word in bzrMergeCommand). Try shell=False and splitting bzrMergeCommand into a list, something like: os.chdir(self._directoryName);bbzrMergeCommand = ["/usr/local/bin/bzr","merge"] Then the subprocess will be spawned in dirName and will launch bzr with the argument merge. (That will leave you in the new directory.) – Perkins Jan 10 '12 at 04:36
  • I've seen this error on windows when it was trying to import libraries from different versions. What versions of python do you have installed? And which is the default? If it is launching 2.6 to run bzr, and is trying to import site from 2.7, it will fail. Also, bzr is failing on import os, so in the python shell, try importing os and make sure it works. – Perkins Jan 12 '12 at 19:31
  • I tried running the script from CLI. It works. So the problem is in eclipse? Thanks. – Ron D. Jan 16 '12 at 08:23
0

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.

Rob Wouters
  • 15,797
  • 3
  • 42
  • 36
0

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

Simon Whitaker
  • 20,506
  • 4
  • 62
  • 79
  • This the error i got after your suggestion: /bin/sh: /usr/local/bin/bzr: /usr/bin/python2.62.6: bad interpreter: No such file or directory – Ron D. Jan 07 '12 at 12:39
  • Looks like you may have either a) already run this command, or b) managed to run it twice in this session? (Or c) installed Bazaar from a source that already had it hard-wired to use python2.6.) In any event, you've ended up replacing the /usr/bin/python in /usr/bin/python2.6 to /usr/bin/python2.6, resulting in /usr/bin/python/2.62.6 which obviously doesn't exist. To fix it, try: `sudo sed -i '' s,/usr/bin/python2.62.6,/usr/bin/python2.6, /usr/local/bin/bzr` – Simon Whitaker Jan 07 '12 at 20:29