Python being the new fashionable language, especially in Finance industry, I started learning it.
I downloaded it from the Python website - Python version 3.5.2 - and it successfully installed... in my Application folder.
Just so you know a little bit more about myself, I did an IT engineering school in France, so I have an IT culture, but I never had the soul of a hacker so some things might be more difficult than others.
Started to code on Python IDE, then I created an executable python file and when I tried to execute it ... error!!
This is what I got when I executed my file (bissextile.py - file is supposed to ask the user to enter a year and tell him if this year is bissextile):
Last login: Tue Aug 9 23:24:02 on ttys000
MacBook-Pro-de-Tebah:~ tebahsaboun$ cd '/Users/tebahsaboun/Desktop/' && '/usr/bin/pythonw' -d -v '/Users/tebahsaboun/Desktop/bissextile.py' && echo Exit status: $? && exit 1
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py
import site # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py
import os # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py
import posixpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.py
import stat # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py
import genericpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.pyc matches
[...] A LOT OF STUFF THAT'S THE SAME AS BEFORE
import encodings.aliases # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py
import encodings.utf_8 # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc
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.
File "/Users/tebahsaboun/Desktop/bissextile.py", line 1
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
^
SyntaxError: unexpected character after line continuation character
# A lot of cleaning up files after that...
So, I saw here two issues:
1) my file doesn't work ...
2) the shell is not using Python 3.5 but Python 2.7 which is the problem I need help for.
Lots of research on internet, I was about to follow that tutorial:
https://wolfpaulus.com/journal/mac/installing_python_osx/comment-page-3/#comment-101285
...and about to delete some system files from my mac :) which it didn't let me do...decided that I didn't really know what I was doing and that I should dig a little more in the internet.
For the record, here is what I have in /System/Library/Frameworks/python.framework/Versions/ :
So I found something called "Homebrew" that is suppose to install Python for you and I followed this great tutorial:
http://blog.manbolo.com/2014/09/27/use-python-effectively-on-os-x#p1
Asking Homebrew to install Python 3.5 instead of Python 2.7 (steps are absolutely the same) which I apparently did.
So I did the verification suggested in the tutorial to be sure that I was using the right version of Python but when I asked the shell I still got Python 2.7.
I checked my /etc/paths file which is as follows:
/usr/local/bin
/usr/bin
/bin
/usr/bin
/bin
/usr/local/bin is the first binary in the file and I checked the folder it contains Python 3.5 indeed. But no matter what is the first line is in that file I get :
MacBook-Pro-de-Tebah:~ tebahsaboun$ which python
/usr/bin/python
and :
**MacBook-Pro-de-Tebah:~ tebahsaboun$ python
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 can't put all the links that I have researched so far ( especially stack overflow articles by the way ;) ) cause I don't have enough "reputation", but I did a lot of research and no article seems to respond to my problem. And I don't understand what is wrong.
Run the program from command line using python3 my_script.py or add shebang #!/urs/bin/python3
I cannot find the test module in my Anaconda's version of Python. Can anyone help me fix this. This module is used by the dpkt library that I am trying to use.
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named test
Quick Fix: You can checkout a copy of pystone.py from the cpython github repository and copy it to a test directory that is present in your PYTHONPATH. Or you would copy it to a test directory in your python project's root (ugly way).
Perhaps Anaconda Python does not ship with a copy of the test module. This is a standard part of Python 2.7. Other times, users accidentally overwrite their Python standard library's test module with something else. You can try to use the Python version that is shipped with OS X instead. If that fails as well, then try to see which test module is being loaded, and go from there.
import test
print test
Alpine Linux ships a python2-tests package.
the quickest way to fetch it if you don't have an Alpine lxc container is from a main repo here (or apk fetch python2-tests inside lxc).
the .apk can be uncompressed with an archiver to a .tar.gz & then just uncompress again.
As you already know I have vim problems.
I usually use python 3 with pymode enabled.
Today I wanted to add a nice autocompletion for python to my vim and the things became messy.
I wanted to install [YouCompleteMe][1] with pathogen, and I saw that requires vim to be installed with python2 support.But pymode needs python3 support (or at least I need it with python3 support)
After a lot of googling I precompiled and installed both python versions with --enable-shared configuration and vim with both supports(python/dyn and python3/dyn)
The problem is that vim doesn't load python3 when needed.
I'm working on a project in python 3 and I tried adding this to the end of file:
import site
print(site.getsitepackages())
The output is:
['/usr/lib/python2.7/site-packages', '/usr/lib/site-python']
And if i type this as vim's command :
:py3 import site; print(site.getsitepackages())
The output is:
['/usr/lib/python3.4/site-packages', '/usr/lib/site-python']
I still haven't try to install YouCompleteMe...
My question is: how can I tell vim to use python3 by default instead of python2 ?
(without recomiling it with only python3 support)
Vim and its plugins are not pre-compiled objects files but are rather callable scripts which often simplify by not specifying version to be used.
If
$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
then vim is using python 2.7.
Changing default python is not a good idea. As i mentioned in my comment .. u should look at temporary option. One of the simplest option (bash-shell):
$ alias python='python3.2'
$ python
Python 3.2.3 (default, Feb 21 2014, 00:48:19)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I'm trying to use python's bitstring module in a script and am getting an import error. This error does not happen when running from interactive mode.
Here's the code:
import bitstring
b = bitstring.BitArray(bin='001001111')
When run like this:
python test.py
I get this:
AttributeError: 'module' object has no attribute 'BitArray'
However, when I do this:
$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bitstring
>>> b = bitstring.BitArray(bin='001001111')
>>> print b
0b001001111
It works just fine! It's the same interpreter being run by the same user. Any pointers?
I predict you have created a bitstring.py in your current directory.
The problem is caused by a bitstring.py file in sys.path of test.py, but not in that of the interactive python shell. Most likely, there's a bitstring.py file in the directory test.py is in, and you started your shell from another working directory.
Since python traverses sys.path from front to end, modules in the current directory - even if accidentally created - overshadow those in system library directories.
Google App Engine actually had a similar issue at one point. The easiest solution there was simply comment the offending line or use try...except. Obviously that won't work here.
In that case, the problem was initialization order. A half second later a similar line of code was called again with success. Their solution? refactor. :-(
The best I've seen is a dynamic lookup of the class: bitstring.__dict__.get("BitArray") or getattr(bitstring, "BitArray");. It isn't ideal (and I believe I've even seen those return null), but hopefully it can get you somewhere.
Does anyone know where i can find this python module 'contextlib'?
root#overo:~# python
Python 2.6.6 (r266:84292, Mar 9 2011, 10:05:36)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named contextlib
I did not compile python myself personally. I'm just in this mess. It's running on an ARM based CPU so maybe some things were left out to save space.
I ran find / | grep contextlib which resulted in nothing.
Can i download this module from somewhere and just plonk it in /usr/lib/python2.6? Will that work?
I got this error in a different way.
I created a pipenv virtual environment using the 32bit version of Python 3.6.5 on Windows 10. I then realized I needed the 64bit version. Uninstalled the 32bit, installed the 64bit, and then tried to go back to my existing virtual env. The previously created env was now broken in odd ways and gave me this error.
I solved this by removing the old pipenv pipenv --rm and creating a new one with the newly installed version of python.
As others have noted, that module should be in the standard library, but if it's an embedded device, it may have been dropped to save space (if true, a foolish choice IMO, since leaving out contextlib.contextmanager robs the with statement of much of its power and convenience)
If you can name the specific device or manufacturer (or ask the vendor directly), you may be able to get a better answer.
As far as fixing it goes, grabbing http://hg.python.org/cpython/file/2.6/Lib/contextlib.py and dropping it in sys.path somewhere should do the trick (running python -m site will dump the list of directories that you can use)
It has been part of the standard library since 2.5 according to the docs. It seems a bit weird that you don't have it, it works with 2.6.6 for me (Ubuntu 10.10):
blair#blair-eeepc:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>> contextlib.__file__
'/usr/lib/python2.6/contextlib.pyc'
Somebody may have a better suggestion, but if it comes to it there is a link at the top of the documentation to the source code (which is Python, so you should be able to use it directly without any compilation or anything).
Edit: Unless, as Santiago Lezica suggested, you compiled your copy of Python manually, in which case it should be a simple matter of copying the module into the correct library path.
Edit for updated question: To the best of my knowledge, just dropping the source into a directory on the Python path should work. You could do this in the system library, but, to avoid it being deleted/replaced/otherwise borked in future updates, I'd recommend putting it in a separate directory and adding that directory to the Python path. You could put it under /usr/local, or somewhere in your home directory.
With Angsrom Linux, contextlib is included in the python-misc package. You can grab it by running:
opkg install python-misc
This won't, however, get you all expected python modules, so you may also want to install python-modules:
opkg install python-modules
I found one more occasion, which produces the same error.
I had made a virtual environment with python 3.6. After a updated my python version to 3.7 I tried to activate the old virtual environment and got this error.
The solution was to delete the old environment and recreate it with the new python version.
Check sys.path to make sure your python interpreter is looking in the right directories. It should look something like this (not necessarily identical):
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/pymodules/python2.6']
EDIT: With the updated information in the question that this is an install of unknown origin on a constrained device, assuming that unnecessary modules were removed to save space makes sense. However, for the record, I'll mention another, perhaps more common scenario where modules cannot be found: when there are file permissions issues. For example:
$ python -c 'import contextlib; print(contextlib.__file__)'
/usr/lib/python2.6/contextlib.pyc
$ ls -l /usr/lib/python2.6/contextlib.py*
-rw-r--r-- 1 root root 4136 Dec 26 16:42 /usr/lib/python2.6/contextlib.py
-rw-r--r-- 1 root root 4127 Jan 1 21:45 /usr/lib/python2.6/contextlib.pyc
$ sudo chmod go-r /usr/lib/python2.6/contextlib.py*
$ python -c 'import contextlib; print(contextlib.__file__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named contextlib
Especially with custom installations, import problems due to file permission issues and path problems are some of the easiest things to check and, usually, to fix.
Python 2
sudo apt-get install python-contextlib2
Python 3
sudo apt-get install python3-contextlib2
contextlib was introduced in Python 2.5, can you remove and re-install your Python 2.6.6 again? From my copy of Python 2.6.6:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>>