当我执行没有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
当我使用sudo执行时,语法错误消失,脚本正确执行。
我该怎么做呢?
修改 请注意,我为每个使用相同的python版本:
$ which python
/Users/judgej4/anaconda3/bin/python
$ sudo which python
/Users/judgej4/anaconda3/bin/python
我注意到PYTHONPATH包含一些普通用户的目录,而sudo没有:
/Users/judgej4/anaconda3/lib/python3.6/site-packages
/usr/local/Cellar/apache-spark/2.2.1/libexec/python
看起来错误来自第一个目录。
我可以从我的PYTHONPATH中删除该目录,但我宁愿解决问题,这似乎与anaconda
编辑2
$ python --version
Python 3.6.3 :: Anaconda custom (64-bit)
$ sudo python --version
Python 3.6.3 :: Anaconda custom (64-bit)
编辑3
$ command -v python
/Users/judgej4/anaconda3/bin/python
$ sudo command -v python
/Users/judgej4/anaconda3/bin/python
答案 0 :(得分:3)
问题不在于你的python解释器 - 它与你的python库设置有关。查看导致异常的行:
File "/Users/judgej4/anaconda3/lib/python3.6/site-packages/concurrent/futures/_base.py", line 381
raise exception_type, self._exception, self._traceback
这是 python 2代码,但它位于 python3.6 / site-packages /...下。看起来你已经在python3中安装了futures
包,这是一个python2包,一个你绝对不需要的反向端口。你提到root的PYTHONPATH不包含
/Users/judgej4/anaconda3/lib/python3.6/site-packages
这就是您的脚本与sudo一起使用的原因:它获取的concurrent
的分发版本位于/Users/judgej4/anaconda3/lib/python3.6/
,而不是futures
中不兼容的/Users/judgej4/anaconda3/lib/python3.6/site-packages
版本。
如果删除了应该解决问题的/Users/judgej4/anaconda3/lib/python3.6/site-packages/concurrent/
目录。
答案 1 :(得分:2)
使用Python 3.6 执行Python 2代码可能导致此错误。
也许您的配置如下所示:
python
命令是python2
python
命令运行python3.6
如果是这样,您应该使用python2
命令来避免这个错误:
sudo python2 main.py
python2 main.py
答案 2 :(得分:0)
sudo看起来正在使用不同版本的python执行你的脚本 - 以下命令将确认这一点:
sudo type python
type python
我希望输出不同,第一个命令打印到安装python 2的路径