1

Got a problem using subprocess.Popen(..)

This command:

HG_REV = subprocess.Popen(["hg", "log", "-l", "1", "../"], stdout=subprocess.PIPE).communicate()[0]

gives me the following Error:

  File "/usr/lib/python3.4/site.py", line 182
    file=sys.stderr)
        ^
SyntaxError: invalid syntax

But this one works fine:

HG_REV = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE).communicate()[0]

Could anyone explain, why this error occurs and how to solve it?

Refered to this post: Mercurial scripting with python

================== UPDATE 1 ============================================== I figured out it has something to do with pydev and anything in my eclipse is probably changing the environment variables. I am pretty sure because running the above commands in a file (python3.4 test.py) on linux shell worked!!

Here are more information:

  1. Ubuntu 14.04.3 on virtual machine
  2. Eclipse Mars
  3. pydev
  4. python3.4 is the current configuration (when I switch to python2.7 it worked)

(I also tried hglib and the exact "same Error" occurs, when running client = hglib.open(my_mercurial_repo) because open is also using subprocess.Popen(..) )

================== UPDATE 2 ============================================== When setting PYTHONPATH to /usr/bin/python3.4 the code works!! But the behavior seems weird to me. What is the common way for using python3.4 on virtual machine? The question is still not solved.

Community
  • 1
  • 1
frank schmidt
  • 121
  • 3
  • 15
  • 1
    You seem to be calling Mercurial (which is a Python2 program) from a Python3 installation. Depending on how your path and environment variables are set, this may be the source of the problem; for example, Mercurial may not react well to PYTHONPATH or PYTHONHOME pointing to Python3 code. As a separate aside, the most convenient way to script Mercurial from Python is probably [hglib](https://www.mercurial-scm.org/wiki/PythonHglib). – Reimer Behrends Oct 16 '15 at 09:39
  • @ReimerBehrends: Nonsense. I've used `hg` from inside a 3.x venv numerous times. It works just fine. Unless OP is using some ancient version of hg or something... – Kevin Oct 16 '15 at 15:04
  • @Kevin: I didn't say that you can't use Mercurial from inside a Python3 program (I've done it myself). I very specifically pointed out that it may have to do with (e.g.) environment variables. Note that the syntax error occurs inside a Python3 installation, so it definitely has *something* to do with that Python3 installation. – Reimer Behrends Oct 16 '15 at 15:12
  • @ReimerBehrends: A venv will set all kinds of environment variables. That's the whole point of *using* a venv. – Kevin Oct 16 '15 at 15:15
  • @Kevin: (1) Nowhere does it say that the author uses a virtual environment (I'm not sure where you get the idea) and (2) one can use virtual environments without sourcing `activate` first (in fact, this is often what you want). – Reimer Behrends Oct 16 '15 at 16:39
  • @Reimer: I fail to see your point. Mercurial can handle a 3.x PYTHON_PATH, as shown by it working in a venv. – Kevin Oct 16 '15 at 16:42
  • @Kevin: Not sure what you are getting at. A virtual environment will *not* set PYTHONPATH or PYTHONHOME. It will clear PYTHONHOME upon sourcing `activate` and set VIRTUAL_ENV (neither of which is necessary for the virtual environment to work). Virtual environments find their imports independently of environment variables. – Reimer Behrends Oct 16 '15 at 17:15
  • @ReimerBehrends How to activate/ set the correct environment variables? PYTHONPAH etc. – frank schmidt Oct 17 '15 at 21:37
  • 2
    @frankschmidt: First check if the environment variables are set at all, e.g. via `subprocess.Popen(["env"])`. If none of them are set, then the underlying reason is something different. If they are set, you should probably track down why they are set in the first place (e.g. in your shell profile) and clear them. If they *are* cleared, then something else is the cause of your problem (which seems to be a Python 2 script trying to use Python 3 code, but could also be a broken Python 3 installation or something like that). – Reimer Behrends Oct 18 '15 at 10:03

0 Answers0