4

When I execute my script without sudo:

$ python main.py 
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    import irc
  File "/Users/judgej4/twitchchat/irc.py", line 3, in <module>
    import asyncio
  File "/Users/judgej4/anaconda3/lib/python3.6/site-packages/asyncio/__init__.py", line 21, in <module>
    from .base_events import *
  File "/Users/judgej4/anaconda3/lib/python3.6/site-packages/asyncio/base_events.py", line 18, in <module>
    import concurrent.futures
  File "/Users/judgej4/anaconda3/lib/python3.6/site-packages/concurrent/futures/__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "/Users/judgej4/anaconda3/lib/python3.6/site-packages/concurrent/futures/_base.py", line 381
    raise exception_type, self._exception, self._traceback
                        ^
SyntaxError: invalid syntax

When I execute with sudo, the syntax error goes away and script executes properly.

How would I go about debugging this?

EDIT Note that I'm using the same python version for each:

$ which python
/Users/judgej4/anaconda3/bin/python
$ sudo which python
/Users/judgej4/anaconda3/bin/python

I did notice that PYTHONPATH contains a few directories for the regular user which sudo does not:

/Users/judgej4/anaconda3/lib/python3.6/site-packages
/usr/local/Cellar/apache-spark/2.2.1/libexec/python

and it looks like the error is coming from the first directory.

I could remove that directory from my PYTHONPATH, but I'd rather fix the problem, which seems to be with anaconda

EDIT 2

$ python --version
Python 3.6.3 :: Anaconda custom (64-bit)
$ sudo python --version
Python 3.6.3 :: Anaconda custom (64-bit)

EDIT 3

$ command -v python
/Users/judgej4/anaconda3/bin/python
$ sudo command -v python
/Users/judgej4/anaconda3/bin/python
3
  • 6
    Run sudo python --version vs python --version and you will probably find that the one without sudo is on Python 2. Commented Feb 3, 2018 at 1:34
  • You should post the output of command -v, not which. Also see Check if a program exists from a Bash script. Commented Feb 3, 2018 at 2:46
  • See edit above. Both are python 3.6.3 Commented Feb 3, 2018 at 21:14

3 Answers 3

3

The problem is not with your python interpreter - it's with your python libraries setup. Look at the line that's causing the exception:

File "/Users/judgej4/anaconda3/lib/python3.6/site-packages/concurrent/futures/_base.py", line 381
    raise exception_type, self._exception, self._traceback

This is python 2 code but it's under python3.6/site-packages/.... It looks like you've installed the futures package in your python3, which is a python2 package, a backport that you definitely don't need. You mention that root's PYTHONPATH doesn't contain

/Users/judgej4/anaconda3/lib/python3.6/site-packages

This is why your script works with sudo: it gets the distribution version of concurrent which is in /Users/judgej4/anaconda3/lib/python3.6/ rather than the incompatible futures version which is in /Users/judgej4/anaconda3/lib/python3.6/site-packages.

If you remove the /Users/judgej4/anaconda3/lib/python3.6/site-packages/concurrent/ directory that should take care of your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

That was it. Thanks for the response.
You're welcome! Please consider accepting the answer (clicking the checkmark) so that if someone else encounters a similar problem they can more easily see the solution.
2

This error can be caused by executing Python 2 code with Python 3.6.

Maybe your configuration looks like this:

  • Your root user's python command is an alias to python2
  • Your regular user's python commands runs python3.6

If so, you should use the python2 command to avoid this mistake:

  • sudo python2 main.py
  • python2 main.py

3 Comments

$ which python /Users/judgej4/anaconda3/bin/python $ sudo which python /Users/judgej4/anaconda3/bin/python
@JosephJudge - Don't use which. Also see Check if a program exists from a Bash script.
Look more closely at the traceback. This is the wrong problem and the wrong solution.
0

sudo looks to be executing your script with a different version of python - the below commands will confirm this:

sudo type python

type python

I expect the outputs to differ, with the first command printing a path to an installation of python 2

5 Comments

which can actually show a different interpreter than the one the shell is using (if there's an alias or function, or if the shell is still using an old PATH lookup but the actual file locations changed). For the non-sudo case, type python is more accurate than which python.
Also see Check if a program exists from a Bash script. You are told to avoid which.
Fair points - I overlooked aliases etc. Edited to provide a more accurate answer.
sudo type doesn't necessarily work, as type is a shell builtin as opposed to an external executable (and sudo directly invokes executables rather than going through in the common case) -- that's why I advised using it specifically for the non-sudo test.
In this case, though, the interpreters really are the same - it's the PYTHONPATH that's the key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.