which version of python is used when I run it in interpreter? - python

I am confused in trying to understand which python version is used when I run them in interpreter?
That is, how the shell decides which python version to load when I have more than one versions.
I have snapshot of my /usr/bin. It has atleast two different python versions2.5 and 2.6.
I have not specified anything in my .bashrc to choose which one from.
when I execute python command on bash shell, python interpreter opens up, but loads with version 2.6.
How is this detected by shell?
If I want to change versions, how will I do that? That is, I want to change the default version
lastly what kind of files are these in /usr/bin/python. I tried to open them in editor and I see some ###. I have read many posts here but some makes me confusing.
ls /usr/bin/python
python python2.5 python2.6 pythonw pythonw2.6
python-config python2.5-config python2.6-config pythonw2.5
python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

To know which version is used by default, type
which python
To use another one, use the full path when invoking them:
/usr/bin/python/python2.5 myfile.py
/usr/bin/python/python2.6 myfile.py
etc.
If you want to change the default, you can create an alias in ~/.bashrc for example:
my_python='/usr/bin/python/python2.7'
or better change the link direction of /usr/bin/python (or whatever comes from which python). It is a link, so link it to the exact version you need.
ln -s /prefered/path/of/python /usr/bin/python
The files you see in /usr/bin/python are executables. You can do the following to see this:
file /usr/bin/python/python2.5
In my case:
$ file /usr/bin/python
/usr/bin/python: symbolic link to `python2.7'
$ file /usr/bin/python2.7
/usr/bin/python2.7: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xf66a10f2c444b2329b25ab6790abb7fbb4fe3f78, stripped

python --version
Then head on over to your .bashrc (should be in your home directory) and add:
alias python='[your path]'
for me I have:
alias python='/usr/bin/python2.7'
Just make sure you say:
source ./bashrc
to apply your changes.

Related

Why can't Anaconda python in shebang line find script (cygwin)?

I use cygwin on Windows 10, and want to write Python code
that uses Pandas and other packages contained in my Anaconda base
environment. I can run Python code if I give the absolute
path to the code, but not if bash needs to use PATH
to find the code.
I use these lines in my .bash_profile as suggested
here (search for 'bash_profile'):
. /opt/conda/etc/profile.d/conda.sh
conda activate base
Here's the start of my PATH, showing Anaconda stuff at the front:
$ echo ${PATH:1:160}
cygdrive/c/Users/Glenn/Anaconda3:/cygdrive/c/Users/Glenn/Anaconda3/Library/mingw-w64/bin:/cygdrive/c/Users/Glenn/Anaconda3/Library/usr/bin:/cygdrive/c/Users/Gle...
I can run Anaconda Python at the bash prompt:
$ which python
/cygdrive/c/Users/Glenn/Anaconda3/python
$ python -i
Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
>>>
I can even run a Python program that uses Pandas:
$ pwd
/home/Glenn/bin
$ cat test.py
#!/usr/bin/env python
import pandas as pd
print(pd.Series([1,2]))
$ ./test.py
0 1
1 2
dtype: int64
The problem is that I can't run the program if I rely on the path:
$ which test.py
/home/Glenn/bin/test.py
$ test.py
C:\Users\Glenn\Anaconda3\python.exe: can't open file '/home/Glenn/bin/test.py': [Errno 2] No such file or directory
$
I get the same kind of error message if I invoke test.py from
another directory using an absolute pathname, but not if I invoke
it using a relative pathname.
I'm a longtime Cygwin user and haven't seen this kind of behavior
before, so suspect it has to do with Anaconda.
I can change to any other directory and get the same behavior.
Also, if I put the absolute path to Anaconda's Python in the
shebang line, I get the same behavior.
I've found (and tried) many suggestions for configuring Anaconda in a Cygwin environment, but many of them are 4 or 5 years old.
UPDATE: I was not able to solve this problem, but have switched to the Linux Windows Subsystem and no longer have to deal with the issue.

Makefile finds wrong python installation (python 2 instead of python 3)

I'm trying to build Pyodide on a Mac and have hit a snag with what appears to be my default python installation. I've tried the solution here, but I'm still getting the following error on building using the makefile:
stg-MBP:pyodide stg$ make
make -C packages
../bin/pyodide buildall . ../build \
--ldflags="-O3 -s "BINARYEN_METHOD='native-wasm'" -Werror -s
EMULATED_FUNCTION_POINTERS=1 -s EMULATE_FUNCTION_POINTER_CASTS=1 -s
SIDE_MODULE=1 -s WASM=1 -s "BINARYEN_TRAP_MODE='clamp'" --memory-init-
file 0" --
host=/Users/stg/workspaces/pyodide/cpython/build/3.7.0/host
--target=/Users/stg/workspaces/pyodide/cpython/installs/python-3.7.0
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named pathlib
/usr/bin/python: No module named pyodide_build
make[1]: *** [all] Error 1
My .bash_profile only contains the following lines:
alias python=python3
# Setting PATH for Python 3.7
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
However, the terminal returns the following responses to these commands
stg-MBP:pyodide stg$ which python
/usr/bin/python
stg-MBP:pyodide stg$ python --version
Python 3.7.0
stg-MBP:pyodide stg$ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
stg-MBP:pyodide stg$ alias
alias python='python3'
I've tried clearing the hash table and restarting terminal, but I still can't get make to find python3 instead of python2 (I'm assuming the culprit is the fact that which python still returns the system python instead of python3.) Is there some way to point the makefile to the correct python3 installation at least for the current terminal session?
.bash_profile is only evaluated by login shells, and aliases are not inherited by subshells, so the alias is not active when the Pyodide build runs.
You either need to edit bin/pyoide in the build tree, or you can put a symbolic link to python3 under the name python in a directory and add this directory to the front of PATH, like this:
PATH="/path/to/directory-with-symbolic-link:$PATH" make
This way, python will invoke the Python 3 interpreter because PATH is inherited by subshells.

Vim with both python/dyn and python3/dyn uses python 2 by default

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.

Why is Code Runner using the old 2.71 version of Python instead of 3.x on OSX?

I am trying to use the newer version of Python but when I type:
import sys
print sys.version_info
I get back:
sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0)
In the terminal when I enter python I get:
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
When I enter python3 I get:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
As you see, I have installed Python 3.3 but no matter what I do I can't seem to actually use it in CodeRunner.
For the latest version of VS Code, you need to open settings (shift+command+p) and override the python interpreter value.
In new versions of the settings.json file, just enter:
"code-runner.
(Note the ") and it should show an auto-complete list (or you can press ctrl+space) and select "code-runner.executorMap". It should show all of the run commands. Change:
"python": "python -u",
to
"python": "python3 -u",
To change your IntelliSense for error handling in python3, open the Command Palette (ctrl+shift+P) and select "Python: Select interpreter" and select python 3.x.x.
CodeRunner->Preferences->Languages->Run Command
edit "python $filename" to "python3 $filename"
Try changing Python's "run command" in the preferences to:
python3 $filename;
Use Command+, to open the Preferences, and then make sure the Languages of Python3 have this:
Right Run Command
BTW, use which python3 to make sure the path is /usr/local/bin/python3 instead of /usr/bin/python3 to ensure safety
Crate a python file and get current sysinfo
import sys
print(sys.version_info)
Need to change to python3?
open your vscode settings file
1.1 mac: command+shift+p
1.2. search for openSettingsJson
locate the python attribute in the json object
change the value to python3
validate results by running the aforementioned file
profit
Another STUPID but working hack is to set Alias in your Shell.
What Code Runner basically does is
python -u filename.py
But if you goto your Shell Config file and add one line at your config file(possibly .bashrc/.zshrc or whatever you use)
i.e
alias python='python3'
now whenever coderunner executes
python -u filename.py
it is actually executing
python3 -u filename.py

Python: No module named contextlib?

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
>>>

Categories

Resources