My mac came with Python2 by default. I installed Python3 using Homebrew (brew install python3) and did something to get python to point to python3 in Terminal. Unfortunately I can't remember what I did (I must have casually copied and pasted a couple lines of commands to get python set to python3). Now that I want to find out what I did that was forgotten, I looked everywhere in my $PATHS, my .bash_profile, .zhrc etc but I found nothing set for python or python3 - no PATHs written, no aliasing either.
How is that possible and what else could I have done?
My Python versions:
my-mac:~ myname$ python2
Python 2.7.16 (default, Feb 28 2021, 12:34:25)
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
my-mac:~ myname$ python
Python 3.9.6 (default, Jun 29 2021, 05:25:02)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Here are all my PATHs:
my-mac:~ myname$ echo "${PATH//:/$'\n'}"
/Users/myname/.rvm/gems/ruby-1.9.3-p327/bin
/Users/myname/.rvm/gems/ruby-1.9.3-p327#global/bin
/Users/myname/.rvm/rubies/ruby-1.9.3-p327/bin
/Users/myname/.rvm/bin
/usr/local/mysql/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin
/Users/myname/Qt/5.5/clang_64/bin/
Checking Python and Python3 paths:
my-mac:~ myname$ type python
python is hashed (/usr/local/bin/python)
my-mac:~ myname$ type python3
python3 is /usr/local/bin/python3
My .bash_profile and .zshrc content:
my-mac:~ myname$ nano .bash_profile
export PATH="/usr/local/mysql/bin:$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
my-mac:~ myname$ nano .zshrc
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
brew install the binaries at /usr/local/bin which is before /usr/bin in your PATH. (brew updates the PATH to include it prior to the systems /usr/bin). You don't need to do anything to put brew installed apps in your PATH, it's already there.
Try which python and see where it points. Here's an example of MacOS default interpreter.
➜ which python
/usr/bin/python
➜ python
Python 2.7.16 (default, Jun 18 2021, 03:23:53)
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
If you want python to point to the system's Python 2.x, you can delete /usr/local/bin/python. Then to use Python3, you'll need to specify python3. (which is located at /usr/local/bin/)
You can list /usr/local/bin which contains symlinks to the actual binaries.
➜ ll /usr/local/bin | grep python
lrwxr-xr-x 1 usr admin 38B Sep 5 12:27 python3 -> ../Cellar/python#3.9/3.9.6/bin/python3
lrwxr-xr-x 1 usr admin 45B Sep 5 12:27 python3-config -> ../Cellar/python#3.9/3.9.6/bin/python3-config
lrwxr-xr-x 1 usr admin 40B Sep 5 12:27 python3.9 -> ../Cellar/python#3.9/3.9.6/bin/python3.9
Related
Yesterday, I did put my laptop on upgrade 19.10 to 20.04 but due to power failure, that became a partial-upgrade, the system broked. I resolved everything but my Django app wasn't running due to PYTHONPPATH so I tried uninstalling python3 and everything got broken. I re-installed that again.
Now when I do python --version I got
bash: python: command not found
whereas python3 --version gives correct answer.
Python 3.8.2
I have python2.7 and python3 both installed. So for now, my Python is not working and also I think I've messed up my PYTHONPATH and I really don't know what I'm going to do now.
My ./~bashrc file looks like below :
# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH
# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH
# Install Ruby Gems to ~/gems
export GEM_HOME="$HOME/gems"
export PATH="$HOME/gems/bin:$PATH"
I'm using Ubuntu 20.04.
Please specify how are you running your project and what exactly is the issue you are facing. May be you can paste the error message you get.
For python command,
In Linux, generally the base commands (like python) without version in it, would actually be pointing the specific (python) version executable through symbolic links (or simply links).
[foo#linuxbox ~]$ ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 16 Feb 9 16:26 /usr/bin/python -> /usr/bin/python3
These links can be created or even edited to our need to point to the version we need. Use the below command to link python to python3. This is equivalent to setting alias for python3 as python but bit more than that as all users/process can run python but in case of alias the tool/user must be running from bash or corresponding shell where alias was created.
sudo ln -f -s /usr/bin/python3 /usr/bin/python
I feel in Ubuntu 20 you have to run command python2 to go into 2.7.* interpreter. python and python3 command both refers to Python3. But anyway your python command should work.
#ideapad:~$ python
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ideapad:~$ python2
Python 2.7.17 (default, Apr 15 2020, 17:20:14)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ideapad:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
To solve your issue, use an alias. Place command alias python=python3 into ~/.bashrc file, after adding this run source ~/.bashrc.
Other solutions:
run command which python it will reveal the location of installed Python and then try adding the location given by which python command to PYTHONPATH
Reinstall your python - sudo apt install python
I had two versions of python installed (python - 2.7.12 and python3 - 3.5.2). After installing python 3.7.7, I wanted to make it the default of the python3 command by making a few commands. But unfortunately, the python3 command no longer works!
fogang#FF:~$ python
Python 2.7.12 (default, Oct 8 2019, 14:14:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
fogang#FF:~$ python3.7
Python 3.7.7 (default, Mar 10 2020, 17:25:08)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
fogang#FF:~$ python3
bash: /usr/lib/command-not-found : /usr/bin/python3 : mauvais interpréteur: Aucun fichier ou dossier de ce type
fogang#FF:~$
Translated: "/usr/bin/python3: bad interpreter: No file or folder of this type"
There is my /usr/bin
fogang#FF:/usr/bin$ ls python*
python python2.7-config python3.5-config python3.7 python3.7m-config python3m-config pythontex3
python2 python2-config python3.5m python3.7-config python3-config python-config
python2.7 python3.5 python3.5m-config python3.7m python3m pythontex
/usr/bin/python3 is missing. Based on the comments, you're using Ubuntu, so the solution is to simply reinstate it:
sudo ln -s python3.5 /usr/bin/python3
Note that changing the system python3 version on Ubuntu will break things, so the better solution is to use an alias, e.g. put alias python3="python3.7" in your bashrc.
I run into this issue ImportError numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS2_AsASCIIString installing Python in a pyenv-virtualenv environment.
In my case, it happens with the matplotlib package instead of numpy (as in the above question), but it's basically the same issue.
The answer given in that question is a simple:
Rebuild NumPy against a Python built as UCS-4.
I don't know how to do this. In this other question it is said that one has to use:
./configure --enable-unicode=ucs4
but I don't know how to use that command along with pyenv.
This issue is also mentioned in pyenv's repo issue list, and a solution given in a comment. Sadly (for me) I can not understand how to apply the fix explained in said comment.
So my question basically is: how do I build Python as UCS-4 via pyenv?
Installing python with pyenv with ucs2:
$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2
$ pyenv install -v 2.7.11
...
$ pyenv local 2.7.11
$ pyenv versions
system
* 2.7.11 (set by /home/nwani/.python-version)
$ /home/nwani/.pyenv/shims/python
Python 2.7.11 (default, Aug 13 2016, 13:42:13)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_config_vars()['CONFIG_ARGS']
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs2' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '"
Installing python with pyenv with ucs4:
$ pyenv uninstall 2.7.11
pyenv: remove /home/nwani/.pyenv/versions/2.7.11? y
$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs4
$ pyenv install -v 2.7.11
...
$ pyenv local 2.7.11
$ pyenv versions
system
* 2.7.11 (set by /home/nwani/.python-version)
$ /home/nwani/.pyenv/shims/python
Python 2.7.11 (default, Aug 13 2016, 13:49:09)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_config_vars()['CONFIG_ARGS']
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs4' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '"
I'm using pyenv, virtualenv, autoenv.
When I activated pyenv, it shows an error.
Chois#Chois-MacPro ~ $pyenv activate chacha_dabang
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(chacha_dabang) Chois#Chois-MacPro ~ $python
pyenv: python3.5: command not found
The `python3.5' command exists in these Python versions:
3.5.1
So, I checked with which:
(chacha_dabang) Chois#Chois-MacPro ~ $which python
/Users/Chois/.pyenv/shims/python
I execute python with this one :
(chacha_dabang) Chois#Chois-MacPro ~ $/Users/Chois/.pyenv/shims/python
Python 3.5.1 (default, Aug 3 2016, 11:09:57)
[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.
>>>
It works well!!!
I don't know why it doesn't work when I type just python
Need your help.
Here is my PATH :
Chois#Chois-MacPro ~ $echo $PATH
/Usr/local/Cellar/pyenv-virtualenv/20160716/shims:/Users/Chois/.pyenv/shims:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/Chois/Library:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Users/Chois/phantomjs-1.9.2-macosx/bin
and .pyenv:
Chois#Chois-MacPro versions $pwd
/Users/Chois/.pyenv/versions
Chois#Chois-MacPro versions $ls
3.5.1 chacha_dabang
~$ python
bash: python: command not found
while running python2 and python 3 its showing different versions.
~$ python2
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
~$ python3
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
i think somehow default python link is broken. i can't seem to access ubuntu software center also.
Please suggest a way to fix this.
Rather than re-creating the simlink as suggested, I would start working with virtualenv. You can create virtual environment pointing at different python versions by doing:
virtualenv -p /usr/bin/python2 venv_name (for Python 2.7)
virtualenv -p /usr/bin/python3 venv_name (for Python 3)
Once you go into the venv and do the source venv_name/bin/activate, your python symbolic link will be pointed at the version you've chosen when creating it.
Also, you might find a pyvenv link which will be associated to Python 3 as this version of Python comes with virtualenv (and pip) out of the box. You might need to install virtualenv with pip (or easy install), and to figure it out which Pythonversion is your pip associated to, you can do pip --version