Unable to install python3.7.x in mac - macos

I am pretty new in mac version 12.6 . I tried to install python 3.7 in my mac for trying out some legacy project but I am unable to.
I tried two was downloading the .pkg file from python.org and also by pyenv
While the .pkg says successfully installed and pyenv versions gives me 3.7.13 .
While trying python3.7 or python3 from terminal I am getting
% python3
zsh: killed python3
% python3.7
zsh: killed python3.7
But my other versions working fine like
% python3.9
Python 3.9.13 (v3.9.13:6de2ca5339, May 17 2022, 11:37:23)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
How to fix this isssue.

I usually use pyenv to install and manage Python versions. (You can install it using Homebrew)
1- pyenv install 3.7.0
#To see the list of the Python versions
2- pyenv versions
#To set the newly installed version as the default
3- pyenv global 3.7.0
Now, run python3 --version
if the output is NOT 3.7.0 then you need to add the following lines to your .zsh or .zshrc file:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
and finally update your .zsh or .zshrc file:
source .zshrc or source .zsh

Related

Unable to run python

Error: Can't find Python executable "python", you can set the PYTHON env variable.
Unable to run python while running service in Intellij.
Running python should work on my mac.
On local mac python2 is no longer available from macOS Monterey by default.
In order to explicitly install python2 please follow the below steps:
brew install pyenv
pyenv install 2.7.18
echo 'export PATH="$(pyenv root)/versions/2.7.18/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

How can I run Python 3.9.1 natively on M1 Mac?

Seems to be impossible currently with Anaconda as well as with Xcode 12. Via idle, it runs via Rosetta. There seems to be no discussion of this so either I'm quite naive or maybe this will be useful to others as well.
Python says: "As of 3.9.1, Python now fully supports building and running on macOS 11.0 (Big Sur) and on Apple Silicon Macs (based on the ARM64 architecture). A new universal build variant, universal2, is now available to natively support both ARM64 and Intel 64 in one set of executables" https://docs.python.org/3/whatsnew/3.9.html
Please help a newbie figure out how to take advantage of his recent impulse-buy.
You can now install python 3.9.1 through multiple pathways now but the most comprehensive build environment for the full data-science suite for python at the moment (Feb 2021) on M1 ARM architecture is via miniforge.
e.g.
brew install --cask miniforge
conda init zsh
conda activate
conda install numpy scipy scikit-learn
You can now install Python 3.9.4 natively on Mac M1 (Apple Silicon). I'm using pyenv to install Python 3.7, 3.8 and 3.9 all native ARM. For example, to install 3.9.4:
$ pyenv install 3.9.4
python-build: use openssl#1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.9.4.tar.xz...
-> https://www.python.org/ftp/python/3.9.4/Python-3.9.4.tar.xz
Installing Python-3.9.4...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.9.4 to /Users/squademy/.pyenv/versions/3.9.4
For a complete guide on install pyenv and multiple Python version, you might read this article: https://squademy.medium.com/install-python-on-macos-11-m1-apple-silicon-using-pyenv-12e0729427a9.
I am using python3.9.4. I installed it using homebrew only.
brew install python#3.9
Also you may want to do the following to unlink and check the version number
brew unlink python3
brew link python3.9
python3 --version
I upgraded to 3.9.4
Download the Python universal installer -
https://www.python.org/downloads/mac-osx/
Note: I still could not get sudo pip install mysqlclient to install.
I had add to
update homebrew - See https://brew.sh
Add /opt/homebrew/bin to PATH in .bash_profile (don't forget to source .bash_profile)
or
Add /opt/homebrew/bin to PATH in .zprofile (don't forget to source .zprofile) if using zsh

Install specific version of python2 with homebrew

I've installed python 2.7.13 with homebrew but I need 2.7.10. It seems something trivial... I've seen similar questions like this or this but I haven't been able to install it.
I've tried to tap homebrew/versions and then do brew search python but there are no versions (rather than python2 and python3).
Homebrew doesn't do this, I would recommend using homebrew to install pyenv and then using pyenv to install and use python 2.7.10, and any other versions you need.
Get pyenv:
brew update
brew install pyenv
Then add eval "$(pyenv init -)" to your .bash_profile and relaunch terminal.
Install python 2.7.10:
pyenv install 2.7.10
You can then set 2.7.10 as the global python by using pyenv global 2.7.10 but I would instead recommend you look at the pyenv virtualenv or pyenv virtualenvwrapper projects and use a python virtual environment for your code, or set the python for your project folder only by cd'ing to your project folder and using pyenv local 2.7.10.

Should I use pip or pip3 to install python3 packages inside of a virtual environment?

I have python 2(as default) and 3 on my Linux machine. I created a virtual environment to try out pandas with python3. So, I ran:
virtualenv -p python3 pandas_env
If I type python inside of this env it goes to the standard python shell, with python 3 as the default version
(panda_env)$ python
Python 3.4.3 (default, Sep 14 2016, 12:36:27)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
To install ipython, I did:
(panda_env)$ pip install ipython
now, running ipython, it defaults to python2 instead of 3
(panda_env)$ ipython
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
Type "copyright", "credits" or "license" for more information.
IPython 2.3.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
With a WARNING saying to install ipython inside virtualenv. I thought did installed ipython inside the environment, and why is it using python2.x by default?
How do I install ipython inside virtualenv correctly, and how can I make ipython use 3.x instead of 2.x?
Also, should I use pip or pip3 to install packages inside a virtualenv?
Note, for python > 3.3 it's recommended you start a virtual environment with the pyvenv command. This has now been deprecated as of python > 3.6 in favor of python3 -m venv <environment name> (via https://docs.python.org/3/library/venv.html)
In answer to your question, I don't believe it matters based on this check:
# start enviroment
$ python3 -m venv test_env; source test_env/bin/activate
# check python version
(test_env) $ python --version
Python 3.6.0
# check pip and pip3
(test_env) $ which pip
~/Documents/test_env/bin/pip
(test_env) $ which pip3
~/Documents/test_env/bin/pip3
# The pip commands are different, but if we check their versions they are the same and both appear to be using python3.
(test_env) $ pip -V
pip 9.0.1 from ~/test_env/lib/python3.6/site-packages (python 3.6)
(test_env) $ pip3 -V
pip 9.0.1 from ~/test_env/lib/python3.6/site-packages (python 3.6)
# In fact the binaries for pip and pip3 within the environment are identical as you can see with this.
(test_env) $ diff ~/Documents/test_env/bin/pip ~/Documents/test_env/bin/pip3
# Checking with ipython
(test_env) $ pip install ipython
(test_env) $ ipython
Python 3.6.0 (default, Dec 24 2016, 08:01:42)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
The easiest way to work with virtual environments is to download anaconda and use the conda version control system:
Works exactly the same way for all versions of python
Python is treated as just another package as part of the environment
Everything related to an environment is stored in one folder
You can see revision history and rollback to previous versions of the environment if something stops working

Installing different Python version in virtualenv with installing version 3.5.1 from source

I'm running openSUSE 13.1 and I'm trying to install Python 3.5.1 in an virtualenv, because the system default is 3.3.5 and cannot be further updated with yast.
I have downloaded the source, stored it in folder ~/pysrc35 and created a new directory ~/localpython and installed it
./configure --prefix=/home/<user>/.localpython
make
make install
the python3 executable there works and the localpython/bin looks like
2to3 easy_install-3.5 idle3.5 pip3.5 pydoc3.5 python3.5 python3.5m python3-config pyvenv-3.5
2to3-3.5 idle3 pip3 pydoc3 python3 python3.5-config python3.5m-config pyvenv
Now I want to use pip, which seems to have been included in the installation, but when I run it I get the error:
ImportError: No module named 'pip'
I have already set $PYTHONHOME to /home/<usr>/localpython/bin/python3 and $PYTHONPATH to /home/<usr>/localpython/lib/python3.5/:/home/<usr>/localpython/lib64/python3.5/:/home/<usr>/localpython/include/python3.5m/, because I got errors on that before, but now I'm stuck with pip.
A great solution for this was using pyenv. This tool makes installing different python versions and virtual environments super easy. The only prerequisite is probably just having git installed. Then you clone it
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
and run
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
eval "$(pyenv virtualenv-init -)"
in terminal. For Linux distributions other than openSUSE or Ubuntu ~/.bashrc might have to be replaced by
~/.bash_profile
. After restarting the terminal, run
pyenv install 3.5.x
to install whatever version you want (except 3.5.1 is not yet available). Then you should switch to your desired Python version, e.g. with
pyenv shell 3.5.x
to set the version for the current terminal session.
After that a virtual environment can be created and activated by
pyenv virtualenv /path/to/venv
pyenv activate /path/to/venv
See also this video for a comprehensive tutorial on using pyenv and in conjunction with django.

Resources