python 2 instead of python 3 as the (temporary) default python? - python

on my computer
~$ python -V
Python 3.2.1
but I get into problems when I run some python programs. my guess is (or at least I want to try this) that there is some backward compatibility issues, and I want to run those python scripts with
python2 2.7.2-2
which is also installed on my system but I do not know how to make it as the (temporary) default python. The python script starts with
#!/usr/bin/env python
and I am using arch linux.

You can use virtualenv
# Use this to create your temporary python "install"
# (Assuming that is the correct path to the python interpreter you want to use.)
virtualenv -p /usr/bin/python2.7 --distribute temp-python
# Type this command when you want to use your temporary python.
# While you are using your temporary python you will also have access to a temporary pip,
# which will keep all packages installed with it separate from your main python install.
# A shorter version of this command would be ". temp-python/bin/activate"
source temp-python/bin/activate
# When you no longer wish to use you temporary python type
deactivate
Enjoy!

mkdir ~/bin
PATH=~/bin:$PATH
ln -s /usr/bin/python2 ~/bin/python
To stop using python2, exit or rm ~/bin/python.

Just call the script using something like python2.7 or python2 instead of just python.
So:
python2 myscript.py
instead of:
python myscript.py
What you could alternatively do is to replace the symbolic link "python" in /usr/bin which currently links to python3 with a link to the required python2/2.x executable. Then you could just call it as you would with python 3.

You don't want a "temporary default Python"
You want the 2.7 scripts to start with
/usr/bin/env python2.7
And you want the 3.2 scripts to begin with
/usr/bin/env python3.2
There's really no use for a "default" Python. And the idea of a "temporary default" is just a road to absolute confusion.
Remember.
Explicit is better than Implicit.

You could use alias python="/usr/bin/python2.7":
bash-3.2$ alias
bash-3.2$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
bash-3.2$ alias python="/usr/bin/python3.3"
bash-3.2$ python
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

If you have some problems with virtualenv,
You can use it:
sudo ln -sf python2 /usr/bin/python
and
sudo ln -sf python3 /usr/bin/python

Use python command to launch scripts, not shell directly. E.g.
python2 /usr/bin/command
AFAIK this is the recommended method to workaround scripts with bad env interpreter line.

As an alternative to virtualenv, you can use anaconda.
On Linux, to create an environment with python 2.7:
conda create -n python2p7 python=2.7
source activate python2p7
To deactivate it, you do:
source deactivate
It is possible to install other package inside your environment.

I think it is easier to use update-alternatives:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1

Related

Command 'python' not found - after I've assigned it as an alias to python3

I'm in the early stages of learning django with python. Have done none of both before.
I'm following a course where the instructor has showed how to start a new project using vagrant, and one of the files in the main directory is called Vagrantfile, no file extension (same for the instructor).
These are the contents of the file:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "ubuntu/bionic64"
config.vm.box_version = "~> 20200304.0.0"
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.provision "shell", inline: <<-SHELL
systemctl disable apt-daily.service
systemctl disable apt-daily.timer
sudo apt-get update
sudo apt-get install -y python3-venv zip
touch /home/vagrant/.bash_aliases
if ! grep -q PYTHON_ALIAS_ADDED /home/vagrant/.bash_aliases; then
echo "# PYTHON_ALIAS_ADDED" >> /home/vagrant/.bash_aliases
echo "alias python='python3'" >> /home/vagrant/.bash_aliases
fi
SHELL
end
From my understanding, this line echo "alias python='python3'" >> /home/vagrant/.bash_aliases tells vagrant that when I use the python command, it should regard it as if I wrote python3.
When I write the following command though python -m venv ~/env, I get in response this message:
Command 'python' not found, but can be installed with:
apt install python3
apt install python
apt install python-minimal
Ask your administrator to install one of them.
I call this command after I've ran the vagrant ssh command and I'm already inside the virtual machine, and navigate to the vagrant dir. I know it because in git bash this shows as my current location vagrant#ubuntu-bionic:/vagrant$.
I'm not sure if it's related, or if it's a different issue, but I'll add that even if I run this command instead python3 -m venv ~/env, I get this response:
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/vagrant/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
If I run the install command it recommends (with sudo), I get this in response:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package python3-venv is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
This blocks my learning, no idea how to move forward with his. Any help is appreciated.
The problem is probably aliasing python3 to python, by default Ubuntu 18 shipped both Python 2 (python) and Python 3 (python3) and changing that carelessly can cause instabilities, ranging from subtle bugs, to your system not booting, you can:
use update-alternatives to change it
use python3 as it is
install the package python-is-python3 (although I'm not sure if this is available in 18)
Anyway, by deleting the alias, everything should go back to normal.
I'm trying to reproduce this on Ubuntu 20.04.
sudo dpkg -i virtualbox-6.1_6.1.14-140239~Ubuntu~eoan_amd64.deb
mkdir vagrant
cd vagrant
vagrant init hashicorp/bionic64
vagrant up
At this point my Vagrantfile contains no alias. See https://github.com/michaelhochleitner/vagrant-test/blob/master/Vagrantfile.
Testing python in vagrant ssh without alias.
vagrant ssh
python
Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Testing python in vagrant ssh with alias.
At this point my Vagrantfile contains the alias from your post. See https://github.com/michaelhochleitner/vagrant-test/blob/master/Vagrantfile_with_alias.
vagrant reload
vagrant ssh
python
Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
I'm assuming the
Command 'python' not found, but can be installed with:
error is not caused by the alias in the Vagrantfile.
Try to resolve the issue by installing python3-venv.
What is the output of python in a terminal outside vagrant ssh?
What is the output of python3 in a terminal outside vagrant ssh?
Which operating system are you using?
Also see https://askubuntu.com/questions/958303/unable-to-create-virtual-environment-with-python-3-6.

MacOS: Multiple python versions installed and unmanageable. Cleanup wanted

I've been on macOS for 2 years now and in the past year I've begun to work a lot in Python via VScode. But lately I have been running into so many problems because I didn't set up python properly from the start. I have multiple versions and modules installed globally (I know that is bad)... But I was wondering if anyone had advice about how I can clean up the Python set up so that there is the latest version being used and all modules will be installed properly.
I used homebrew as well and that is just adding to the mess. I want to do this right so that I can stop messing with configurations every day and just be able to develop.
I'll include some basic terminal outputs but if there is more that anyone would like to see I would be happy to provide more detail. If starting from scratch is the best thing to do then I'll do it. I don't know my way around all the configuration files and pathing so I'll need some help if that is what I'll have to do.
$ which python
/usr/bin/python
$ which python3
/usr/local/bin/python3
$ python --version
Python 2.7.16
$ python3 --version
Python 3.7.7
$ python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> tensorflow.__file__
'/usr/local/lib/python3.7/site-packages/tensorflow/__init__.py'
VScode has the following interpreters available (not sure if that helps)
2.7.16 /usr/bin/python
2.7.16 /System/Library/Frameworks....
3.7.3 /usr/bin/python3
3.7.7 /usr/local/bin/python3
3.7.7 /usr/local/opt/python/bin/python3.7
Any help would be awesome!! I'm just tired of fighting with this and wanted to ask for help
Having multiple versions of python is not really a propblem per se.
What I recommend is :
# In $HOME/.bashrc or .zshrc
PATH=/usr/local/bin:$PATH
cd /usr/local/bin
ln -fs python3 python
# Once the first and this step done, when you type [python],
# you'll be using /usr/local/bin/python3
As the first line of your python scripts, put :
#!/usr/bin/env python
This way, you ensure your are always using the version /usr/local/bin/python3

terminal runs python 2.7 when I have python 3.5 installed on mac.

I was installing, uninstalling and reinstalling pythons on my mac,
and I think things are screwed up a little.
At first by default, terminal ran Python 3.5 when I typed
$python
, but after doing some things, it installed 2.7 and now the terminal runs python 2.7 instead of 3.5
I installed python 3.5 form http://python.org/.
When I open up bash_profile
$vim ~/.bash_profile
This is what shows up
# virtualenv
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
Now when I type:
$ python
This shows up:
Python 2.7.11 (default, Jun 23 2016, 17:25:20)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
How can I reset all the python stuff(remove older versions, etc) to the factory version that i had when I first bought Mac(python3.5)?
This worked for me:
Python 3.x
python3
Python 2.x
python
in terminal
To see what command is actually going to run when you type python at the prompt, check the top result of:
type -a python
This will list any aliases as well as taking into account the full PATH definition. To figure out why python 2 is getting precedence over python 3, be sure to check your ~/.bashrc file if it exists as well as your ~/.bash_profile.
To check your Python binaries, run:
$ which -a python python2 python3
Then check which python path comes first.
Then either set your $PATH or $PYTHONPATH (then reload your shell), or use python2 or python3 command instead.
You can also use following workaround:
PATH="/usr/bin:$PATH" ./python_script.py
where /usr/bin is pointing to the right Python binary.

Installed Python from Package, Terminal didn't update?

Terminal is still showing Python 2.7.2 after an install of 3.3.0
I'm new to python- just want to get a good development environment working on Mac 10.8.
Use python3 instead of python:
$ python3
Python 3.2.3 (default, Oct 19 2012, 19:53:57)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
See PEP 394:
This PEP provides a convention to ensure that Python scripts can continue to be portable across *nix systems, regardless of the default version of the Python interpreter (i.e. the version invoked by the python command).
python2 will refer to some version of Python 2.x
python3 will refer to some version of Python 3.x
python should refer to the same target as python2 but may refer to python3 on some bleeding edge distributions
You have Python 2 and Python 3 installed, so the python command refers to python2. If you want Python 3, do it explicitly with the python3 command.
You have few options
In your bash ~/.bash_profile add alias python='python3'
Instead of using python command use python3 instead.
Install python3 via homebrew

python location on mac osx

I'm a little confused with the python on osx. I do not know if the previous owner of the laptop has installed macpython using macport. And I remembered that osx has an builtin version of python. I tried using type -a python and the result returned
python is /usr/bin/python
python is /usr/local/bin/python
However running both python at these locations give me [GCC 4.2.1 (Apple Inc. build 5646)] on darwin. Do they both refer to the same builtin python mac provided?
I also read that installing macpython one would
A MacPython 2.5 folder in your Applications folder. In here you
find IDLE, the development environment that is a standard part of
official Python distributions...
I looked at Applications, and theres a MacPort folder with python2.6 and the mentioned stuff in it. But running IDLE, i find the same message as above.
Hmm I'm a little confused. Which is which?
This one will solve all your problems not only on Mac but
to find it on Linux also ( & every basic shell).
TL;DR (you don't have to go through all the answer - just the 1st half).
LET'S GO
Run in terminal:
which python3
On Mac you should get:
/usr/local/bin/python3
WAIT!!! It's prob a symbolic link, how do you know? Run:
ls -al /usr/local/bin/python3
and you'll get (if you've installed Python w/ Brew):
/usr/local/bin/python3 -> /usr/local/Cellar/python/3.6.4_4/bin/python3
which means that your
/usr/local/bin/python3
is actually pointing to (the real location)
/usr/local/Cellar/python/3.6.4_4/bin/python3
That's it!
Longer version (optional):
If for some reason, your
/usr/local/bin/python3
is not pointing to the place you want, which in our case:
/usr/local/Cellar/python/3.6.4_4/bin/python3
just back it up (+cool trick to add .orig suffix to file):
cp /usr/local/bin/python3{,.orig}
and run:
rm -rf /usr/local/bin/python3
now create a new symbolic link:
ln -s /usr/local/Cellar/python/3.6.4_4/bin/python3 /usr/local/bin/python3
and now your
/usr/local/bin/python3
is pointing to
/usr/local/Cellar/python/3.6.4_4/bin/python3
Check it out by running:
ls -al /usr/local/bin/python3
I found the easiest way to locate it, you can use
which python
it will show something like this:
/usr/bin/python
[GCC 4.2.1 (Apple Inc. build 5646)] is the version of GCC that the Python(s) were built with, not the version of Python itself. That information should be on the previous line. For example:
# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/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.
>>>
# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Items in /usr/bin should always be or link to files supplied by Apple in OS X, unless someone has been ill-advisedly changing things there. To see exactly where the /usr/local/bin/python is linked to:
$ ls -l /usr/local/bin/python
lrwxr-xr-x 1 root wheel 68 Jul 5 10:05 /usr/local/bin/python# -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python
In this case, that is typical for a python.org installed Python instance or it could be one built from source.
On Mac OS X, it's in the Python framework in /System/Library/Frameworks/Python.framework/Resources.
Full path is:
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Btw it's easy to find out where you can find a specific binary: which Python will show you the path of your Python binary (which is probably the same as I posted above).
I checked a few similar discussions and found out the best way to locate all python2/python3 builds is:
which -a python python3
On High Sierra
which python
shows the default python but if you downloaded and installed the latest version from python.org you can find it by:
which python3.6
which on my machine shows
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
installed with 'brew install python3', found it here
Run this in your interactive terminal
import os
os.path
It will give you the folder where python is installed
i found it here:
/Library/Frameworks/Python.framework/Versions/3.6/bin
which python3 simply result in a path in which the interpreter settles down.
run the following code in a .py file:
import sys
print(sys.version)
print(sys.executable)
I have a cook recipe for finding things in linux/macos
First update the locate db then do a
locate WHATiWANTtoSEARCH | less
do a /find to find what you are looking for.
to update your locate db in macos do this:
sudo /usr/libexec/locate.updatedb
it sometimes takes a while. Hope this helps :)

Categories