python runs older version after installing updated version on Mac - python

I am currently running python 3.6 on my Mac, and installed the latest version of Python (3.11) by downloading and installing through the official python releases. Running python3.11 opens the interpreter in 3.11, and python3.11 --version returns Python 3.11.0, but python -V in terminal returns Python 3.6.1 :: Continuum Analytics, Inc..
I tried to install again via homebrew using brew install python#3.11 but got the same results.
More frustrating, when I try to open a virtual environment using python3 -m venv env I get
Error: Command '['/Users/User/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
I altered .bash_profile with
# Setting PATH for Python 3.11
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}"
export PATH
. "$HOME/.cargo/env"
And created a .zprofile based on this post with
export PYTHONPATH=$HOME/Users/User
and a .zshrc based on this post, but --version still throws python3.6.
I'm running Big Sur OS. Pip and homebrew are up to date and upgraded. Acknowledging that I'm totally foolish, what do I need to do to get python >3.7 running in terminal?

What you want to do is overwrite a python symlink.
After installing python via homebrew, you can see that python3.11 is just symlink.
cd /usr/local/bin; ls -l | grep python3.11
The result is:
lrwxr-xr-x 1 user admin 43 Nov 7 15:43 python3.11# -> ../Cellar/python#3.11/3.11.0/bin/python3.11
So let's just overwrite it.
ln -s -f $(which python3.11) $(which python)
ln -s -f $(which python3.11) $(which python3)
ln -s -f $(which pip3.11) $(which pip)
ln -s -f $(which pip3.11) $(which pip3)
After these commands, pip, pip3, python3, python will invoke the version 3.11.
This command makes soft symlink.
ln -s
This command with -f option overwrite an existing soft symlink.
A soft symlink is similar to a shortcut.
In man page,which command is described as
which - shows the full path of (shell) commands.

Related

Python is installed but is not working in terminal

I installed Python 3 using the Cygwin terminal. There were no issues during installation.
I get this:
$ which python3
/usr/bin/python3
and
$ whereis python3
python3: /usr/bin/python3 /usr/bin/python3.6 /usr/lib/python3.6 /usr/lib/python3.8
/usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
But neither of
$ python3 $ python3 main.py $ python3 --version
give any output. I have done this the same way before on the same computer and it worked fine. What did I do wrong this time? Or forgot to do?
Try to create an alias to attach python to the common name "python3". In your Cygwin terminal write:
$ alias python3=$(which python3.6)
python3 should be a link managed by alternatives pointing to python3.8
$ alternatives --display python3
python3 - status is auto.
link currently points to /usr/bin/python3.8
/usr/bin/python3.6 - priority 36
/usr/bin/python3.7 - priority 37
/usr/bin/python3.8 - priority 38
Current `best' version is /usr/bin/python3.8.
that belongs to python38 package
$ cygcheck -f /usr/bin/python3.8
python38-3.8.7-3
verify that python38 is correctly installed
$ cygcheck -c python38
Cygwin Package Information
Package Version Status
python38 3.8.7-3 OK
and that there are no issue on the dependency
$ cygcheck /usr/bin/python3.8.exe
D:\cygwin64\bin\python3.8.exe
D:\cygwin64\bin\cygwin1.dll
C:\WINDOWS\system32\KERNEL32.dll
C:\WINDOWS\system32\ntdll.dll
C:\WINDOWS\system32\KERNELBASE.dll
D:\cygwin64\bin\libpython3.8.dll
D:\cygwin64\bin\cygintl-8.dll
D:\cygwin64\bin\cygiconv-2.dll
D:\cygwin64\bin\cyggcc_s-seh-1.dll
More likely you need to re-install python38 or one of the needed libraries
$ cygcheck -f /usr/bin/cygwin1.dll /usr/bin/cygintl-8.dll /usr/bin/cygiconv-2.dll /usr/bin/cyggcc_s-seh-1.dll
cygwin-3.1.7-1
libgcc1-10.2.0-1
libiconv2-1.16-2
libintl8-0.19.8.1-2
be sure to stop all Cygwin processes before re-installing the packages; specially any running service

create the python soft link fails

I have multiple Python versions, and thus also include multiple Python binary executables.
ls /usr/bin/python*
shows the python environments in my ubuntu 18.04
/usr/bin/python /usr/bin/python2.7 /usr/bin/python2-pbr /usr/bin/python3 /usr/bin/python3.5m /usr/bin/python3.6-config /usr/bin/python3.6m-config /usr/bin/python3m
/usr/bin/python2 /usr/bin/python2-jsonschema /usr/bin/python2-wsdump /usr/bin/python3.5 /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3-config /usr/bin/python3m-config
in order to satisfy the PyFlink requirement regarding the Python environment version, i have to choose to soft link python to point to your python3 interpreter:
ln -s /usr/bin/python3 python
however when I use the command, it tells me
ln: failed to create symbolic link 'python': File exists
so i wonder do I need to delete the /usr/bin/python first and then use the command to create the soft link?
Command ln -s /usr/bin/python3 python creates link ./python pointing to /usr/bin/python3. You probably executed it multiple times so ./python already exists. You could overwrite it by providing -f flag to ln.
You definitely should not delete /usr/bin/python.
The whole idea with manually creating links to Python interpreter to install a package seems very weird. I suggest one of the following options:
just use the full path to the interpreter: /usr/bin/python3.6 -m pip install <package>; if the package adds any scripts globally usable from command line, like pyspark, they will be installed with hashbang pointing to the interpreter you installed them with
use a virtual environment: /usr/bin/python3.6 -m venv ~/.env-py36; source ~/.env-py36/bin/activate; python -m pip install <package>
use something like pyenv

How to set Python 3.8 as my default python version on kali linux

I installed python 3.8 in my kali linux OS, but when I type python in kali linux terminal it showing like this
> Python 2.7.18 (default, Apr 20 2020, 20:30:41)
How can I set Python 3.8 as my default?
Hope this answers your question.
The easiest way would be to run alias python=python3 .
So, now if you run the command python it will load python3 instead of python while being under the same alias/command python.
What I usually do is install non-default versions of programs under /usr/local. Then I configure the file $HOME/.bash_profile so that the $PATH variable has /usr/local/bin first, before /usr/bin and /bin (which might actually be the same physical directory on your system).
$HOME/.bash_profile:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# PATH starts with...
PATH="/usr/local/bin"
# add $HOME/bin if it exists...
if [ -d "${HOME}/bin" ]; then
PATH="${PATH}:${HOME}/bin"
fi
# These should exist, add them as fall back....
PATH="${PATH}:/bin:/usr/bin:/sbin:/usr/sbin"
# Some custom locations last
if [ -d /usr/local/arm-elf/bin ]; then
PATH="${PATH}:/usr/local/arm-elf/bin"
fi
if [ -d /opt/local/bin ]; then
PATH="${PATH}:/opt/local/bin"
fi
This has the effect of making anything located in /usr/local/bin the default when I am using an interactive shell, and letting the system defaults remain in effect otherwise.
first, check if you have python3.8 installed
use
ls /usr/bin/python*
then check for an alternative python version using
update-alternatives --list python
if the output of python3.8 is set to 2
To set the default python command to run python3.8
use this to create a persistent symlink
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
the 2 depends on your python alternatives
then
python --version
First remove python 2.7.18
Type >> sudo apt purge -y python2-minimal
install python latest version
Type >> sudo apt install -y python3-pip
latest version of python is installed!

zsh: abort python error when I try to run the app in venv

I setup Python 3.6 using pyenv so I could manage multiple Python versions (e.g. 3.7 and 3.8) in the future. I didn't use Homebrew to install Python since it changes the system version. It's my first time to use zsh shell since it's the default shell in Catalina OS. Currently, I use 3.6.8 version for my existing project.
So here's my current setup:
% pyenv versions
result:
system
* 3.6.8 (set by /Users/macbook/.python-version)
3.7.3
% python -V results to Python 3.6.8
% which python results to /Users/macbook/.pyenv/shims/python
% echo $PATH results to /Users/macbook/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
The content of my ~/.zshrc is PATH=$(pyenv root)/shims:$PATH
I created a virtual env using % python -m venv venv, installed all the necessary packages, and when I activate it and get the python path,
(venv) % python -V
Python 3.6.8
(venv) % which python
/Users/macbook/python-project/venv/bin/python
(venv) % echo $PATH
/Users/macbook/python-project/venv/bin:/Users/macbook/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Finally, when I try to run the app, I always get zsh: abort error:
(venv) % python app.py
zsh: abort python app.py
(venv) % export FLASK_APP=app.py
(venv) % flask run
zsh: abort flask run
I don't know what else is still missing or are there anything wrong with my python path?
Thanks!
After searching through the web, I think this is a common issue with the latest MacOS or Homebrew. This thread fixed the issue.
brew update && brew upgrade && brew install openssl
copy the two files from /usr/local/Cellar/openssl#1.1/1.1.1g to /usr/local/lib/
cd /usr/local/Cellar/openssl#1.1/1.1.1g/
sudo cp libssl.1.1.1.dylib libcrypto.1.1.1.dylib /usr/local/lib/
add symlink to missing openssl libs
cd /usr/local/lib
sudo ln -s libssl.1.1.1.dylib libssl.dylib
sudo ln -s libcrypto.1.1.1.dylib libcrypto.dylib
For me, the below worked:
Python 3.6.9
MacOs Catalina 10.15.7
cd /usr/local/Cellar/openssl#1.1/1.1.1h/
cp lib/libssl.1.1.dylib lib/libcrypto.1.1.dylib /usr/local/lib
cd /usr/local/lib
sudo ln -s libssl.1.1.dylib libssl.dylib
sudo ln -s libcrypto.1.1.dylib libcrypto.dylib
Thanks to the answer by Zhanrah

Installing distribute in Python 3.3 venv (OS X/Homebrew)

I've been trying to get up and running with the built-in "venv" module of Python 3.3 on my OS X machine. I've installed Python 3.3 using Homebrew.
As per the docs, creating and switching virtual environment works as you'd expect:
$ python3 -m venv myvenv
$ source myvenv/bin/activate
And I've tested something like this:
$ echo "YEAH = 'YEAH!'" > myvenv/lib/python3.3/site-packages/thingy.py
$ python
>>> import thingy
>>> print(thingy.YEAH)
'YEAH!'
But when I try to install distribute, it simply won't go in the proper place. For some reason, it insists on trying to install into /usr/local/lib/python3.3/site-packages/, which fails with the following messages:
No setuptools distribution found
running install
Checking .pth file support in /usr/local/lib/python3.3/site-packages/
/Users/victor/myvenv/bin/python -E -c pass
TEST FAILED: /usr/local/lib/python3.3/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from. The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/usr/local/lib/python3.3/site-packages/
and your PYTHONPATH environment variable currently contains:
''
This happens regardless if I try to install using distribute_setup.py or using the source distribution directly. I've even tried using --prefix=/Users/victor/myenv but it still tries to put everything in my "global" site-packages.
I can't figure out why this happens, but it's consistent on two of my machines. Note that sys.prefix reports the correct path (the virtual environment).
Is this a problem with Homebrew? OS X? Python 3.3? venv? Me?
This has been an issue with Homebrew, yes, but it is working now since https://github.com/mxcl/homebrew/commit/0b50110107ea2998e65011ec31ce45931b446dab.
$ brew update
$ brew rm python3 #if you have installed it before
$ brew install python3
$ cd /tmp
$ which python3
/usr/local/bin/python3
$ python3 -m venv myvenv
$ source myvenv/bin/activate
$ wget http://python-distribute.org/distribute_setup.py # may need brew install wget
$ python3 distribute_setup.py
...
Finished processing dependencies for distribute==0.6.45
After install bootstrap.
Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info
Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools.pth
You see that distribute install successfully into the /tmp dir.
This happens because homebrew installs distutils config file:
$ brew cat python3 | grep "Tell distutils" -A5
# Tell distutils-based installers where to put scripts
(prefix/"Frameworks/Python.framework/Versions/#{VER}/lib/python#{VER}/distutils/distutils.cfg").write <<-EOF.undent
[install]
install-scripts=#{scripts_folder}
install-lib=#{site_packages}
EOF
$ mv ~/.local/Frameworks/Python.framework/Versions/3.3/lib/python3.3/distutils/distutils.cfg ~/tmp/
$ cat ~/tmp/distutils.cfg
[install]
install-scripts=/Users/gatto/.local/share/python3
install-lib=/Users/gatto/.local/lib/python3.3/site-packages
$ . venv/bin/activate
(venv) $ python distribute-0.6.36/distribute_setup.py
(venv) $ ls venv/lib/python3.3/site-packages/
distribute-0.6.36-py3.3.egg easy-install.pth setuptools-0.6c11-py3.3.egg-info setuptools.pth
See "distutils.cfg Can Break venv" issue at bugs.python.org.

Categories

Resources