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
  • 6
    Run `sudo python --version` vs `python --version` and you will probably find that the one without `sudo` is on Python 2. – metatoaster Feb 03 '18 at 01:34
  • You should post the output of `command -v`, not `which`. Also see [Check if a program exists from a Bash script](https://stackoverflow.com/q/592620/608639). – jww Feb 03 '18 at 02:46
  • See edit above. Both are python 3.6.3 – Joseph Judge Feb 03 '18 at 21:14

3 Answers3

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.

Nathan Vērzemnieks
  • 5,170
  • 1
  • 8
  • 21
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
Ronan Boiteau
  • 8,035
  • 6
  • 32
  • 47
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

stuartgm
  • 89
  • 3
  • `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`. – Charles Duffy Feb 03 '18 at 02:21
  • Also see [Check if a program exists from a Bash script](https://stackoverflow.com/q/592620/608639). You are told to avoid `which`. – jww Feb 03 '18 at 02:44
  • Fair points - I overlooked aliases etc. Edited to provide a more accurate answer. – stuartgm Feb 03 '18 at 02:50
  • `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. – Charles Duffy Feb 03 '18 at 22:29
  • In this case, though, the interpreters really are the same - it's the PYTHONPATH that's the key. – Nathan Vērzemnieks Feb 03 '18 at 22:49