Can't use Python because of some error homebrew.pth - python-2.7

Whenever I try to run python, I get this error relating to my path.
FrankieMacBook-Pro-2~$ python
Error processing line 2 of /Users/Frankie/Library/Python/2.7/lib/python/site-packages/homebrew.pth:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 152, in addpackage
exec line
File "<string>", line 1
import site; site.addsitedir("/usr/local/lib/python2.7/site-packages") mkdir -p /Users/Frankie/Library/Python/2.7/lib/python/site-packages
^
SyntaxError: invalid syntax
Remainder of file ignored
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
I've been using Python3 which doesn't have this issue for me, but now I have a task where I need to use Python2, so need to face this issue.
Any ideas what my issue is?
Did I somehow screw up my path with homebrew?

Your problem is that the Python interpreter runs site.py on startup. It is importing homebrew.pth and the copy of this file in your 2.7 installation is mangled.
Find this file and either delete it or use a text editor to take this code
mkdir -p /Users/Frankie/Library/Python/2.7/lib/python/site-packages
out of the line that begins
import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")
If you get further syntax errors after doing this, look at the ^ in the syntax error message. It will point to anything else the interpreter does not like.

Related

python ImportError: No module named Tkinter

Every time I tried to run import matplotlib,
I have error ImportError: No module named Tkinter.
The output result is as listed below:
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/usr/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
ImportError: No module named Tkinter
I tried to install the tk.x86_64 and tk-devel.x86_64 packages, and tried to reinstall the python package but the error still appeared.
Also I tried to import Tkinter but I got the following error.
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Tkinter
Does anyone have a workaround for this error?
I'm using python2.7.12 on ubuntu16.04.
I installed it with this command:
sudo apt-get install python-tk
I tried to install tkinter package for python2.7.5 from the following link:
tkinter package
Also I found there is dependency library libTix.so()(64bit) for tkinter package and i got it from the following link: libTix.so()(64bit) package
after that i installed both then I could import Tkinter and import matplotlib.pyplot as plt with no errors.
My 2 cents. Open a terminal and type the following.
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>>
If you do not get an error like below then your Tkinter is fine and the problem is with matplotlib. Then try removing and reinstalling matplotlib.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named tkinter
I was getting the error "ImportError: No module named '_tkinter'" for python3.5 in-spite of installing python3-tk and tkinter-dev.
It showed the error in following file:
File "/usr/local/lib/python3.5/tkinter/init.py", line 35, in
import _tkinter # If this fails your Python may not be configured for Tk
It got resolved for me by simply running the python from /usr/bin like /usr/bin/python3.5 from terminal!
In short, please ensure the python version being run from terminal has the tkinter installed and configured :)
My PC configuration: Ubuntu16.04 with default python2.7 and python3.5
I faced the same issue on a RHEL 7 machine. There was no python-tk package that could be installed. I found the solution here which solved the issue for me:
sudo yum install tkinter
This should work for RHEL machines using Python 2.

Error in Spark installation --pyspark

I am installing spark 1.2.1 on windows 8 and I have downloaded a prebuilt package for Hadoop 2.4
When i am running pyspark i am getting the following error:
C:\Users\Dinesh\Desktop\spark-1.2.1-bin-hadoop2.4>bin\pyspark
Running python with PYTHONPATH=C:\Users\Dinesh\Desktop\spark-1.2.1-bin-hadoop2.4\bin\..\python\lib\py4j-0.8.2.1-src.zip;C:\Users\Dinesh\Desktop\spark-1.2.1-bin-hadoop2.4\bin\..\python;
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
The system cannot find the path specified.
Traceback (most recent call last):
File "C:\Users\Dinesh\Desktop\spark-1.2.1-bin-Hadoop2.4\bin\..\python\pyspark\shell.py", line 45, in <module>
sc = SparkContext(appName="PySparkShell", pyFiles=add_files)
File "C:\Users\Dinesh\Desktop\spark-1.2.1-bin-hadoop2.4\python\pyspark\context.py", line 102, in __init__
SparkContext._ensure_initialized(self, gateway=gateway)
File "C:\Users\Dinesh\Desktop\spark-1.2.1-bin-hadoop2.4\python\pyspark\context.py", line 212, in _ensure_initialized
SparkContext._gateway = gateway or launch_gateway()
File "C:\Users\Dinesh\Desktop\spark-1.2.1-bin-hadoop2.4\python\pyspark\java_gateway.py", line 73, in launch_gateway
raise Exception(error_msg)
Exception: Launching GatewayServer failed with exit code 1!
Warning: Expected GatewayServer to output a port, but found no output.
I have searched and i got that in general the error is caused as the path variable are not correctly defined,but i have checked and my variable are all in place. How can i solve the error?
"The system cannot find the path specified." Which path is it talking about.?
In my case the problem came from the terminal I was using. On Git Bash on Windows I was getting the error : line 96: CMD: bad array subscript when executing spark-shell, but when I tried on PowerShell it worked fine.
The way I debugged this issue was to rem the "#echo off" command on all the command files that got called by pyspark.cmd.
In the end I nailed it down to me having JAVA_HOME set to "C:\ProgramData\Oracle\Java\javapath" which is wrong as one of the cmd scripts adds a "\bin" to JAVA_HOME before calling java.exe and it was triggering the "The system cannot find the path specified." error.
So I changed JAVA_HOME to "C:\Program Files\Java\jdk1.8.0_25" and it worked fine.
Now I have to un-rem the "#echo off"s. Hope it helps!
It maybe caused by cygwin in the DOS classpath. Spark uses the find command in the file 'spark-class2.cmd', which used then the cygwin find command instead of the DOS find command, which works somewhat different. I removed cygwin from the DOS PATH, which solved the problem.

python-magic WindowsError: access violation writing 0x00000000

I installed python-magic (0.4.6) on my Win 7 64bit using pip.
I then installed cygwin 1.7.33-2 to provide the needed dlls and created a copy of cygmagic-1.dll named magic1.dll (see
When I run the Python 2.7.6 32bit shell, the "import magic" works fine.
However, a
magic.from_file('c:\user\username\sample.txt')
gives me a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Python27\lib\site-packages\magic.py", line 119, in from_file
m = _get_magic_type(mime)
File "c:\Python27\lib\site-packages\magic.py", line 107, in _get_magic_type
i = instances.__dict__[mime] = Magic(mime=mime)
File "c:\Python27\lib\site-packages\magic.py", line 55, in __init__
self.cookie = magic_open(flags)
WindowsError: exception: access violation writing 0x00000000
Any ideas what causes the this error and how I can fix it?
Thank you for your help!
The GnuWin32 file package has a 32-bit magic1.dll, along with its dependencies regex2.dll and zlib1.dll. I know from testing that this version works with python-magic. Here's an overview of the steps that I took to test this in 32-bit Python 3.3.
Extract the files to GnuWin32's installation directory:
C:\Temp>set "GNU=C:\Program Files (x86)\GNU"
C:\Temp>7z x -y -o"%GNU%" file-5.03-bin.zip > nul
C:\Temp>7z x -y -o"%GNU%" file-5.03-dep.zip > nul
Set up the environment to find the DLLs and magic file:
C:\Temp>set PATH=%PATH%;%GNU%\bin
C:\Temp>set MAGIC=%GNU%\share\misc\magic
Install python-magic:
C:\Temp>py -3.3-32 -m pip install python-magic
Collecting python-magic
Downloading python-magic-0.4.6.tar.gz
Installing collected packages: python-magic
Running setup.py install for python-magic
Successfully installed python-magic-0.4.6
Verify that it works:
C:\Temp>py -3.3-32
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12)
[MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, magic
>>> magic.from_file(sys.executable)
b'PE32 executable for MS Windows (console) Intel 80386 32-bit'

python 2.7 + RHEL 6.5 + Shapely-1.4.4 : MemoryError

I am facing this problem over two of my deployments on RHEL 6.5 . Could not find any answers over Google search
Base Installation Packages
(nout)$ sudo rpm -qa | grep geos
geos-devel-3.3.2-1.el6.x86_64
geos-3.3.2-1.el6.x86_64
geos-python-3.3.2-1.el6.x86_64
Shapely Installation
(nout)$ easy_install -Z Shapely-1.4.4.tar.gz
Processing Shapely-1.4.4.tar.gz
Writing /tmp/easy_install-iLylTY/Shapely-1.4.4/setup.cfg
Running Shapely-1.4.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-iLylTY/Shapely-1.4.4/egg-dist-tmp-xFmed4
Numpy or Cython not available, shapely.vectorized submodule not being built.
shapely/speedups/_speedups.c: In function ‘__pyx_pf_7shapely_8speedups_9_speedups_2geos_linestring_from_py’:
shapely/speedups/_speedups.c:1603: warning: assignment discards qualifiers from pointer target type
shapely/speedups/_speedups.c:2087: warning: assignment discards qualifiers from pointer target type
shapely/speedups/_speedups.c:2553: warning: assignment discards qualifiers from pointer target type
shapely/speedups/_speedups.c: In function ‘__pyx_pf_7shapely_8speedups_9_speedups_4geos_linearring_from_py’:
shapely/speedups/_speedups.c:3118: warning: assignment discards qualifiers from pointer target type
shapely/speedups/_speedups.c:3127: warning: pointer targets in passing argument 3 of ‘GEOSCoordSeq_getSize_r’ differ in signedness
/usr/include/geos_c.h:321: note: expected ‘unsigned int *’ but argument is of type ‘int *’
shapely/speedups/_speedups.c:3614: warning: assignment discards qualifiers from pointer target type
shapely/speedups/_speedups.c:4158: warning: assignment discards qualifiers from pointer target type
shapely/speedups/_speedups.c: At top level:
shapely/speedups/_speedups.c:924: warning: ‘__pyx_f_7shapely_8speedups_9_speedups_get_geos_context_handle’ defined but not used
shapely/speedups/_speedups.c:1010: warning: ‘__pyx_f_7shapely_8speedups_9_speedups_geos_from_prepared’ defined but not used
zip_safe flag not set; analyzing archive contents...
shapely.geos: module references __file__
Adding Shapely 1.4.4 to easy-install.pth file
Python Shell
(nout)$ python
Python 2.7.8 (default, Sep 12 2014, 14:39:38)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from shapely.geometry import Polygon, Point
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/omd/nocout/nout/lib/python2.7/site-packages/Shapely-1.4.4-py2.7-linux-x86_64.egg/shapely/geometry/__init__.py", line 4, in <module>
from .base import CAP_STYLE, JOIN_STYLE
File "/omd/nocout/nout/lib/python2.7/site-packages/Shapely-1.4.4-py2.7-linux-x86_64.egg/shapely/geometry/base.py", line 9, in <module>
from shapely.coords import CoordinateSequence
File "/omd/nocout/nout/lib/python2.7/site-packages/Shapely-1.4.4-py2.7-linux-x86_64.egg/shapely/coords.py", line 8, in <module>
from shapely.geos import lgeos
File "/omd/nocout/nout/lib/python2.7/site-packages/Shapely-1.4.4-py2.7-linux-x86_64.egg/shapely/geos.py", line 194, in <module>
error_h = EXCEPTION_HANDLER_FUNCTYPE(error_handler)
MemoryError
Python Shell
>>> from shapely.ops import transform
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/omd/nocout/nout/lib/python2.7/site-packages/Shapely-1.4.4-py2.7-linux-x86_64.egg/shapely/ops.py", line 13, in <module>
from shapely.geos import lgeos
File "/omd/nocout/nout/lib/python2.7/site-packages/Shapely-1.4.4-py2.7-linux-x86_64.egg/shapely/geos.py", line 194, in <module>
error_h = EXCEPTION_HANDLER_FUNCTYPE(error_handler)
MemoryError
On calling the import MemoryError is received
I am not able to understand the reason.
For RHEL + selinux enabled + /tmp as noexec, any module that tries to access and execute in /tmp would generate a MEMORY ERROR.
in my case, /tmp was noexec and selinux was enforced
The /tmp defaults to defaults,noexec,nosuid,nodev.
I set : looking at : https://bugzilla.redhat.com/show_bug.cgi?id=645193#c11
$ getsebool -a | grep httpd_tmp_exec
httpd_tmp_exec --> on
Still the problem persisted, if I am to understand correctly this was because of : https://bugzilla.redhat.com/show_bug.cgi?id=582009
The libffi library tries to write to /tmp,
which isn't allowed for the apache user in a default SELinux config
(https://bugzilla.redhat.com/show_bug.cgi?id=582009).
In this environment, importing ctypes always throws a MemoryError
So the option httpd_tmp_exec --> on was of no use to me.
I went looking into python lib files, searching for /tmp declaration, and i found tempfile.py line 147 : def _candidate_tempdir_list()
There in I saw that if there are envnames TMPDIR , TEMP, TMP , python will pickup the temp location.
Which variable to set ? For that I went in libffi source code, libffi-3.0.6 . file : src/closures.c line 290 open_temp_exec_file_env, "TMPDIR", 0 .
The TMPDIR was the candidate to be set.
I went ahead with mounting /opt/app/tmp, set export TMPDIR=/opt/app/tmp
Doing all this was working fine, till I used python idle, when I went ahead with nginx + uwsgi deployment, I was caughtup by the same error. Now I am not sure why that was happening, I thought must be a uwsgi execution issue. So I reinstalled uwsgi first, then I modified the /location for nginx.conf and looking at : http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html#uwsgi_temp_path set : uwsgi_temp_path
I restarted all the services, nginx, uwsgi. and tried. It worked.
I also first tried with : (safe) HACK : Python ctypes MemoryError in fcgi process from PIL library
Results were disappointing for me.

Error Psycopg2 Mac OS X Mountain Lion Symbol not found: _GSS_C_NT_HOSTBASED_SERVICE

I'm getting a psycopg2 error after updating to Mac OS X Mountain Lion, previously it was working fine.
This is the error if I try to import the module:
Python 2.7.3 (default, Sep 27 2012, 21:57:16)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.65))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/__init__.py", line 67, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _GSS_C_NT_HOSTBASED_SERVICE
Referenced from: /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/_psycopg.so
Expected in: flat namespace
in /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/_psycopg.so
I've used brew to install python and psycopg2.
Any idea would be helpful.
Thanks
Alex
Try brewing postgresql too. The error is probably in the way the libpq (the PostgreSQL client-side library, of which psycopg is a wrapper) is linked.
Mixing native and brew app is not that good... I would rather uninstall psycopg2 and python from brew and install the "Command Line Tools", like explained in pip install fails with /usr/bin/clang: No such file or directory. Then retry pip install, it will work, at least it did the job for me !