Suddenly my Django project showed me the following ImportError: "ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?".
It ran fine before.
I work on Mac m1 using PyCharm Professional.
My Django is installed. My venv is activated. So, I guess I have problem with PYTHONPATH .
My interpreter is exactly I would like to use:
Actually I have some versions of Python. which Python shows me:
(venv) user_name#computer_name project_name % which python
python: aliased to /usr/bin/python3
(venv) user_name#computer_name project_name % which python3
/Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv/bin/python3
(venv) user_name#computer_name project_name % which python3.9
/opt/homebrew/bin/python3.9
python3 -c "import sys; print(sys.path)" shows me:
/opt/homebrew/Cellar/python#3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python310.zip
/opt/homebrew/Cellar/python#3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10
/opt/homebrew/Cellar/python#3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload
/Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv/lib/python3.10/site-packages
If I understand this output correctly, it means that there is no python 3.9 in my PYTHONPATH for now.
In my settings.py, there are:
import os
import sys #not active
from pathlib import Path
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
which $PYTHONPATH
brings me PYTHONPATH not found.
echo $PYTHONPATH
brings me nothing.
The export:
export PYTHONPATH=$PYTHONPATH:/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
didn't help.
OK, I dunno if writing an answer is a good approach, but I may have some suggestions and I need a clean slate to write it in. So...
First, let's forget about Python 3.9 and Python 3.10 and PYTHONPATH for a sec.
Second, let's try to find where django is installed. This looks for a bin that django uses/delivers.
mdfind -name django-admin.py
which returned
/Users/me/kds2/issues2/500.macos/010.fixed.p1.bin_old_py/obsolete/opt.python#3.8/django-admin.py
👉/Users/me/kds2/venvs/bme/bin/django-admin.py 1️⃣
👉/Users/me/kds2/venvs/bme/lib/python3.10/site-packages/django/bin/django-admin.py 2️⃣
/usr/local/lib/python3.8/site-packages/django/bin/django-admin.py
I know my own environment, so I know what's interesting are the 2 under /venvs/bme.
In fact, I know 1️⃣, in /bin/ basically loads 2️⃣ in /site-packages/
Your django will be structured the same way, just in another location.
From your posting so far, I assume your own venv is at
/Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv
So, cd to its bin (which is also probably where you ran source activate)
cd /Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv/bin
type the following command
ls -l
This is what it gets me (after I trim off some files from the output):
-rw-r--r-- 1 me staff 1991 8 Jan 2022 activate
-rw-r--r-- 1 me staff 917 8 Jan 2022 activate.csh
-rw-r--r-- 1 me staff 2059 8 Jan 2022 activate.fish
-rwxr-xr-x 1 me staff 242 24 Jun 14:04 pip
-rwxr-xr-x 1 me staff 242 24 Jun 14:04 pip3
-rwxr-xr-x 1 me staff 242 24 Jun 14:04 pip3.10
lrwxr-xr-x 1 me staff 21 8 Jan 2022👉python -> /opt/local/bin/python
lrwxr-xr-x 1 me staff 6 8 Jan 2022 python3 -> python
lrwxr-xr-x 1 me staff 6 8 Jan 2022👉python3.10 -> python
OK, the important this is that these are symlinks pointing to the actual Python that is driving/hosting this venv. My python is pointing to /opt/local/bin/python because I installed via macports but yours ought to be pointing to homebrews.
Maybe /opt/homebrew/Cellar/python#3.10 ?
And my venv is using Python3.10, as you can see. I expect yours will be as, rather than 3.9.
OK, now let's see what that venv has set up as its environment?
./python3 -m site doc about command
Notice the leading dot. What this does is ignore PATH and whatever and just run this local python3.
What it outputs for me is a whole of useful information, about this virtualenv.
sys.path = [
'/Users/me/kds2/venvs/bme/bin',
'/Users/me/Library/Python/3.10/lib/python/site-packages/_pdbpp_path_hack',
'/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip',
'/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10',
'/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload',
'/Users/me/kds2/venvs/bme/lib/python3.10/site-packages', 👈
'/Users/me/kds2/mygithub/pynoorm',
'/Users/me/kds2/mygithub/bmedev',
'/Users/me/kds2/mygithub/pip_stripper',
'/Users/me/kds2/py2',
'/Users/me/kds2/mygithub/lazy-regression-tests/lazy_regression_tests',
'/Users/me/Library/Python/3.10/lib/python/site-packages',
'/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages',
]
USER_BASE: '/Users/me/Library/Python/3.10' (exists)
USER_SITE: '/Users/me/Library/Python/3.10/lib/python/site-packages' (exists)
ENABLE_USER_SITE: True
That sys.path list above is location at which import will try to find things. Look for ones ending with /site-packages`.
And for those, try to see if there is a django package. Like this.
ls -d /Users/me/kds2/venvs/bme/lib/python3.10/site-packages/django
Sure enough, in the same location where I found my django-admin before.
/Users/jluc/kds2/venvs/bme/lib/python3.10/site-packages/django
Now, I don't know how pycharm and 3.9 figure into this, but that should give a little bit of an idea how your venv is put together.
Maybe you'd be better off reinstalling another venv with django and starting afresh. Your code wouldn't affected. Your call.
The debian I am using has its default python3 -> 3.7.3.
Then I successfully installed the python3 version 3.7.4, which is a dedicated version I prefer to.
But now, the python version goes to mess.
Here is the detailed info from terminal commands
python3 --version
--> Python 3.7.4
/usr/bin/python3 -- version
--> Python 3.7.3
/usr/bin/python3.7 -- version
--> Python 3.7.3
So how can I align it with "Python 3.7.4"?
What AnsFourtyTwo said is correct, but if you need to manage multiple Python versions, try to use pyenv.
It's a tool that lets to manage multiple Python versions (and implementations) on your machine and switch between them easily so that you won't have to change the links manually.
It's similar to nvm (Node.js version manager).
If you have a look into /usr/bin, you will see, that you'll have many different executables for different python versions, e.g. python2.7, python3.7.3, python3.7.4, etc.
To give users the security of a known environment, you'll also find symbolic links, e.g.
lrwxrwxrwx 1 root root 9 Okt 18 2016 python -> python2.7*
lrwxrwxrwx 1 root root 9 Okt 18 2016 python2 -> python2.7*
-rwxr-xr-x 1 root root 3546104 Nov 19 10:35 python2.7*
...
lrwxrwxrwx 1 root root 9 Okt 18 2016 python3 -> python3.5*
-rwxr-xr-x 2 root root 4460336 Nov 17 20:23 python3.5*
So in the example the commands python and python2 will both execute python2.7. Calling python3 will actually execute python3.5.
I usually change those symbolic links to fit my needs (you might need root rights). For example, I usually want the command python to run the current python3 version:
cd /usr/bin
ln -sf python3 python
Why calling python3 --version results in version 3.7.4 must further be examined:
What does which python3 show?
Have you defined any aliases (e.g. alias python="/usr/bin/python3.7.4" in your .bashrc file)
I want to check whether python 3 is installed on my Ubuntu. I'm using this script:
#!/usr/bin/env sh
#install firefox if does not exist
if ! command -v python3 >/dev/null 2>&1
then
echo "not installed"
else
echo "installed"
fi
When I run this script, the output is installed, but when I check my python using python --version, I get this output:
Python 2.7.17
Which, as far as I know, means that the latest version of python on my Ubuntu is 2.7 not 3.x. What's wrong?
The output of command -v python3; echo $?:
/usr/bin/python3
0
The output of ls -l /usr/bin/python3:
lrwxrwxrwx 1 root root 9 Nov 14 09:13 /usr/bin/python3 -> python3.7
The output of ls -l /usr/bin/python3.7:
-rwxr-xr-x 2 root root 5102632 Oct 27 11:43 /usr/bin/python3.7
The output of which python:
/usr/bin/python
The output of ls -l /usr/bin/python:
lrwxrwxrwx 1 root root 7 Jan 19 08:04 /usr/bin/python -> python2
The output of ls -l /usr/bin/python2:
lrwxrwxrwx 1 root root 9 Jan 19 08:04 /usr/bin/python2 -> python2.7
Also I have another Ubuntu on a separate VM, the output of python --version, returns command 'python' not found, but when I execute the above commands for this VM as well, it returns similar responses (indicating that python is installed).
https://www.python.org/dev/peps/pep-0394/
Note: You can have an environment where you have both Python 2 and Python 3 installed, and python can point to either. (Most likely python2 for backward compatibility purpose.) Python 3 applications should always use the python3 command and not python.
Try the whereis command. It will tell you where python3 is.
whereis python
Or better yet
whereis python3
The output should be a path, or a list of paths. You might see something like:
python3: /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python3.6m-config
/usr/bin/python3 /usr/bin/python3.6-config /usr/lib/python3.6 /usr/lib/python3
/usr/lib/python3.7 /usr/lib/python3.8 /etc/python3.6 /etc/python3 /usr/local
Then make sure your PATH variable has at least one of the directories above within it. The PATH environment variable is essentially a list of directories that contain executables (programs). When you type something on your command line your terminal program will search the directories listed in PATH for the executable you specified.
echo $PATH
/home/USER/.pyenv/shims:/home/USER/.pyenv/bin:/home/USER/esp/xtensa-esp32-
elf/bin:/home/USER/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
You might have a directory in your PATH that is messing things up, or you might need to add a new directory.
Because so many programs have been written using Python 2, many operating systems keep Python 2 in their repository, and this isn't going to change any time soon.
So when you installed Python, it added Python 2 to /usr/bin/ (maybe /usr/bin/python2, maybe /usr/bin/python2.7, etc.), and pointed /usr/bin/python to the same location. When you installed Python 3 it also installed Python 3, to /usr/bin/python3.
When you test whether python3 is installed, you find that it is. According to PEP 394, /usr/bin/python should refer to Python 2. Ubuntu documentation explains what that means and what it doesn't:
What this does not mean:
/usr/bin/python will point to Python 3. No, this is not going to happen (unless PEP 394 advocates otherwise, which is doubtful for the foreseeable future). /usr/bin/python and /usr/bin/python2 will point to Python 2.7 and /usr/bin/python3 will point to the latest supported Python 3 version.
Python 2 will be removed from the archive. No, this is not going to happen. We expect Python 2.7 to remain supported and available in Ubuntu for quite a long time, given that PEP 373 promises upstream bug fix maintenance support until 2020. It would be nice if we could demote Python 2 to universe, but that's currently problematic for technical reasons relating to multi-Python version support in Debian/Ubuntu.
Basically, while all development should be geared toward Python 3, the python command (/usr/bin/python) should point to Python 2 in order to keep current programs from breaking.
If you'd like to access Python 3, it's recommended that you call python3. (You could also rebind /usr/bin/python to point to python3, but this is highly unrecommended. A more useful solution to most users would be to create an alias to python3.)
Short version: Your script works. Python 3 is installed. If you want the terminal to open Python 3 when you type python, add an alias alias python=python3.
I have this serious problem with my fedora installation. I was trying to use one of my application that requires Python3. So, as a new Linux user, I simply installed python3. Hence my system for two python versions (3 & 2.7). I guess python3 replaced python2.7. Hence "yum" stopped working. I tried to link /usr/bin/python to python2.7 and removing "PYTHONPATH" from .bashrc.
Now when i am running-
$python -V
Python2.7.8
When I try to run python itself it returns as -
$ python
File "/usr/lib64/python3.4/site.py", line 176
file=sys.stderr)
^
SyntaxError: invalid syntax
when I am running yum I get following result -
$yum
File "/usr/lib64/python3.4/site.py", line 176
file=sys.stderr)
^
SyntaxError: invalid syntax
I am thinking of removing all the python versions from the terminal and then reinstall.
Can somebody help me with this situation? Please help me to correct my system!
Thank you
Amol
After reading a bit onto your variables, it seems like you have a syslink pointing to a weird place that holds a python interpreter that is not the default interpreter.
The output of your
#ls -al /usr/bin/python
lrwxrwxrwx 1 root root 24 Oct 23 16:37 /usr/bin/python -> /etc/alternatives/python
that exactly shows what i mean, what you need to do is make that syslink point to the right python interpreter that should be at /usr/local/bin/python2.7 -> python2.7
so the previous command should give you a:
#ls -al /usr/bin/python
lrwxrwxrwx 1 root root 24 Oct 23 16:37 /usr/bin/python -> python2.7
that could be done with a syslink create command, check the proper documentation to create or modify an existing syslink in fedora 21
I have installed Python 3.2 in my Mac. After I run /Applications/Python 3.2/Update Shell Profile.command, it's confusing that when I type Python -V in Terminal it says that Python 2.6.1.
How can I change the default Python version?
[updated for 2021]
(Regardless if you are on Mac, Linux, or Windows:)
If you are confused about how to start the latest version of python, on most platforms it is the case that python3 leaves your python2 installation intact (due to the above compatibility reasons); thus you can start python3 with the python3 command.
Historically...
The naming convention is that generally, most scripts will call python2 or python3 explicitly. This happened due to a need for backwards compatibility.
Even though technically python doesn't even guarantee backwards compatibility between minor versions, Python3 really breaks backwards compatibility. At the time, programs invoking 'python' were expecting python2 (which was the main version at the time). Extremely old systems may have programs and scripts which expect python=python2, and changing this would break those programs and scripts.
At the time this answer was written, OP should not have changed this due to maintaining compatibility for old scripts.
Circa year 2021...
Nowadays, many years after the python2->python3 transition, most software explicitly refers to python2 or python3 (at least on Linux). For example, they might call #!/usr/bin/env python2 or #!/usr/bin/env python3. This has for example (python-is-python3-package) freed up the python command to be settable to a user default, but it really depends on the operating system.
The prescription for how distributions should handle the python command was written up in 2011 as PEP 394 -- The "python" Command on Unix-Like Systems. It was last updated in June 2019.
Basically if you are writing a library, you should specify the version of python (2 or 3, or finer-grained under specific circumstances) you can use. Otherwise as an end user, you should feel free to rename this for your own personal use (though your OS or distribution may not make that easy).
Shell alias:
You could, however, make a custom alias in your shell. The way you do so depends on the shell, but perhaps you could do alias py=python3, and put it in your shell startup file. This will only work on your local computer (as it should), and is somewhat unnecessary compared to just typing it out (unless you invoke the command constantly).
Confused users should not try to create aliases or virtual environments or similar that make python execute python3; this is poor form.This is acceptable nowadays, but PEP 394 suggests encouraging users to use a virtualenv instead.
Different 3.* versions, or 2.* versions:
In the extremely unlikely case that if someone comes to this question with two python3 versions e.g. 3.1 vs 3.2, and you are confused that you have somehow installed two versions of python, this is possibly because you have done manual and/or manual installations. You can use your OS's standard package/program install/uninstall/management facilities to help track things down, and perhaps (unless you are doing dev work that surprisingly is impacted by the few backwards-incompatible changes between minor versions) delete the old version (or do make uninstall if you did a manual installation). If you require two versions, then reconfigure your $PATH variable so the 'default' version you want is in front; or if you are using most Linux distros, the command you are looking for is sudo update-alternatives. Make sure any programs you run which need access to the older versions may be properly invoked by their calling environment or shell (by setting up the var PATH in that environment).
A bit about $PATH
sidenote: To elaborate a bit on PATH: the usual ways that programs are selected is via the PATH (echo $PATH on Linux and Mac) environment variable. You can always run a program with the full path e.g. /usr/bin/🔳 some args, or cd /usr/bin then ./🔳 some args (replace blank with the 'echo' program I mentioned above for example), but otherwise typing 🔳 some args has no meaning without PATH env variable which declares the directories we implicitly may search-then-execute files from (if /usr/bin was not in PATH, then it would say 🔳: command not found). The first matching command in the first directory is the one which is executed (the which command on Linux and Mac will tell you which sub-path this is). Usually it is (e.g. on Linux, but similar on Mac) something like /usr/bin/python which is a symlink to other symlinks to the final version somewhere, e.g.:
% echo $PATH
/usr/sbin:/usr/local/bin:/usr/sbin:usr/local/bin:/usr/bin:/bin
% which python
/usr/bin/python
% which python2
/usr/bin/python2
% ls -l /usr/bin/python
lrwxrwxrwx 1 root root 7 Mar 4 2019 /usr/bin/python -> python2*
% ls -l /usr/bin/python2
lrwxrwxrwx 1 root root 9 Mar 4 2019 /usr/bin/python2 -> python2.7*
% ls -l /usr/bin/python2.7
-rwxr-xr-x 1 root root 3689352 Oct 10 2019 /usr/bin/python2.7*
% which python3
/usr/bin/python3
% ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mar 26 2019 /usr/bin/python3 -> python3.7*
% ls -l /usr/bin/python3.7
-rwxr-xr-x 2 root root 4877888 Apr 2 2019 /usr/bin/python3.7*
% ls -l /usr/bin/python*
lrwxrwxrwx 1 root root 7 Mar 4 2019 /usr/bin/python -> python2*
lrwxrwxrwx 1 root root 9 Mar 4 2019 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3689352 Oct 10 2019 /usr/bin/python2.7*
lrwxrwxrwx 1 root root 9 Mar 26 2019 /usr/bin/python3 -> python3.7*
-rwxr-xr-x 2 root root 4877888 Apr 2 2019 /usr/bin/python3.7*
lrwxrwxrwx 1 root root 33 Apr 2 2019 /usr/bin/python3.7-config -> x86_64-linux-gnu-python3.7-config*
-rwxr-xr-x 2 root root 4877888 Apr 2 2019 /usr/bin/python3.7m*
lrwxrwxrwx 1 root root 34 Apr 2 2019 /usr/bin/python3.7m-config -> x86_64-linux-gnu-python3.7m-config*
lrwxrwxrwx 1 root root 16 Mar 26 2019 /usr/bin/python3-config -> python3.7-config*
lrwxrwxrwx 1 root root 10 Mar 26 2019 /usr/bin/python3m -> python3.7m*
lrwxrwxrwx 1 root root 17 Mar 26 2019 /usr/bin/python3m-config -> python3.7m-config*
sidenote2: (In the rarer case a python program invokes a sub-program with the subprocess module, to specify which program to run, one can modify the paths of subprocesses with sys.path from the sys module or the PYTHONPATH environment variable set on the parent, or specifying the full path... but since the path is inherited by child processes this is not remotely likely an issue.)
Check the location of python 3
$ which python3
/usr/local/bin/python3
Write alias in bash_profile
vi ~/.bash_profile
alias python='/usr/local/bin/python3'
Reload bash_profile
source ~/.bash_profile
Confirm python command
$ python --version
Python 3.6.5
On Mac OS X using the python.org installer as you apparently have, you need to invoke Python 3 with python3, not python. That is currently reserved for Python 2 versions. You could also use python3.2 to specifically invoke that version.
$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ cd /Library/Frameworks/Python.framework/Versions/3.2/bin/
$ ls -l
total 384
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 2to3# -> 2to3-3.2
-rwxrwxr-x 1 root admin 140 Feb 20 11:14 2to3-3.2*
lrwxr-xr-x 1 root admin 7 Apr 28 15:51 idle3# -> idle3.2
-rwxrwxr-x 1 root admin 138 Feb 20 11:14 idle3.2*
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 pydoc3# -> pydoc3.2
-rwxrwxr-x 1 root admin 123 Feb 20 11:14 pydoc3.2*
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3*
lrwxr-xr-x 1 root admin 12 Apr 28 15:51 python3-32# -> python3.2-32
lrwxr-xr-x 1 root admin 16 Apr 28 15:51 python3-config# -> python3.2-config
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 python3.2-32*
lrwxr-xr-x 1 root admin 17 Apr 28 15:51 python3.2-config# -> python3.2m-config
-rwxrwxr-x 1 root admin 25784 Feb 20 11:14 python3.2m*
-rwxrwxr-x 1 root admin 1865 Feb 20 11:14 python3.2m-config*
lrwxr-xr-x 1 root admin 10 Apr 28 15:51 pythonw3# -> pythonw3.2
lrwxr-xr-x 1 root admin 13 Apr 28 15:51 pythonw3-32# -> pythonw3.2-32
-rwxrwxr-x 1 root admin 25624 Feb 20 11:14 pythonw3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 pythonw3.2-32*
If you also installed a Python 2 from python.org, it would have a similar framework bin directory with no overlapping file names (except for 2to3).
$ open /Applications/Python\ 2.7/Update\ Shell\ Profile.command
$ sh -l
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ cd /Library/Frameworks/Python.framework/Versions/2.7/bin
$ ls -l
total 288
-rwxrwxr-x 1 root admin 150 Jul 3 2010 2to3*
lrwxr-x--- 1 root admin 7 Nov 8 23:14 idle# -> idle2.7
-rwxrwxr-x 1 root admin 138 Jul 3 2010 idle2.7*
lrwxr-x--- 1 root admin 8 Nov 8 23:14 pydoc# -> pydoc2.7
-rwxrwxr-x 1 root admin 123 Jul 3 2010 pydoc2.7*
lrwxr-x--- 1 root admin 9 Nov 8 23:14 python# -> python2.7
lrwxr-x--- 1 root admin 16 Nov 8 23:14 python-config# -> python2.7-config
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 python2.7*
-rwxrwxr-x 1 root admin 1663 Jul 3 2010 python2.7-config*
lrwxr-x--- 1 root admin 10 Nov 8 23:14 pythonw# -> pythonw2.7
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 pythonw2.7*
lrwxr-x--- 1 root admin 11 Nov 8 23:14 smtpd.py# -> smtpd2.7.py
-rwxrwxr-x 1 root admin 18272 Jul 3 2010 smtpd2.7.py*
Old question, but alternatively:
virtualenv --python=python3.5 .venv
source .venv/bin/activate
Update 11 May 2022 Wed EST PM 06:36
Thanks #Aditya Deshpande! Great suggestion!
I've just fixed my answer.
After taking ~/.bashrc or ~/.zshrc effect, there is no need to
quit and restart the terminal.
Do right thing, do thing right!
--->Zero Open your terminal,
--Firstly input python -V, It likely shows:
Python 2.7.10
-Secondly input python3 -V, It likely shows:
Python 3.7.2
--Thirdly input where python or which python, It likely shows:
/usr/bin/python
---Fourthly input where python3 or which python3, It likely shows:
/usr/local/bin/python3
--Fifthly add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile under Bash or ~/.zshrc under zsh.
alias python='/usr/local/bin/python3'
OR
alias python=python3
-Sixthly input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.
--Seventhly then checkout the version of Python
input python -V, It likely shows:
Python 3.7.2
I had done successfully try it.
Others, the ~/.bash_profile under zsh is not that ~/.bash_profile.
The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.
Help you guys!
Change the "default" Python by putting it ahead of the system Python on your path, for instance:
export PATH=/usr/local/bin:$PATH
Set Python 3.5 with higher priority
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
Check the result
sudo update-alternatives --config python
python -V
Check the execution path of python3 where it has libraries
$ which python3
/usr/local/bin/python3 some OS might have /usr/bin/python3
open bash_profile file and add an alias
vi ~/.bash_profile
alias python='/usr/local/bin/python3' or alias python='/usr/bin/python3'
Reload bash_profile to take effect of modifications
source ~/.bash_profile
Run python command and check whether it's getting loading with python3
$ python --version
Python 3.6.5
According to a quick google search, this update only applies to the current shell you have open. It can probably be fixed by typing python3, as mac and linux are similar enough for things like this to coincide. Link to the result of google search.
Also, as ninjagecko stated, most programs have not been updated to 3.x yet, so having the default python as 3.x would break many python scripts used in applications.
I am using OS X 10.7.5 and Python 3.4.2. If you type python3 and what you want to run it will run it using python 3. For example
pyhton3 test001.py. That ran a test program I made called test001. I hope this helps.
you can change temporarily or switch between different versions using following commands:
set path=C:\Users\Shaina\AppData\Local\Programs\Python\Python35-32;%PATH%
python --version
Navigate to:
My Computer -> Properties -> Advanced -> Environment Variables -> System Variables
Suppose you had already having python 2.7 added in path variable and you want to change default path to python 3.x
then add path of python3.5.x folder before python2.7 path.
open cmd: type "python --version"
python version will be changed to python 3.5.x
sudo mv /usr/bin/python /usr/bin/python2
sudo ln -s $(which python3) /usr/bin/python
This will break scripts, but is exactly the way to change python. You should also rewrite the scripts to not assume python is 2.x. This will work regardless of the place where you call system or exec.
In short: change the path in Environment Variables!
For Windows:
Advanced System Settings > Advance (tab). On bottom you'll find 'Environment Variables'
Double-click on the Path. You'll see path to one of the python installations, change that to path of your desired version.
In my case, on my Mac OSX, with Python 2.7.18 installed via mac ports, I was able to set the python version to 2.7 with:
$ sudo port select --set python python27
So:
$ python -V
Python 2.7.18
It should be noted that recent versions of Homebrew/MacOS will require a different entry for the PATH as the location where Homebrew installs Python has changed. Add this like to your .zshrc:
export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"
This will ensure that the appropriate unversioned python link installed by Homebrew appears before that of version 2.x and will protect you from being impacted by any version updates to python as brew will update the unversioned links whenever it updates the version.
Starting with macOS Catalina the default shell is zsh. Therefore, all those ~/.bash_profile changes are not going to change the default when you open a new terminal since the new terminal is a zsh shell and not a bash shell.
You can confirm your terminal is a zsh shell by typing echo $SHELL and you should see a response of: zsh.
What should you do? You should use a solution that works for zsh shell and bash shell. Therefore, do the following:
enter: vi ~/.bash_profile
enter: alias python='python3'
close and save your bash_profile. Enter: :wq
Now open/create your zsh profile. Enter: vi ~/.zshrc
source your bash inside your zshrc. Enter: source ~/.bash_profile
Now python will be aliased to python3 in your zsh shells (and in bash if you switch the default) automatically.
Test:
python --version
Python 3.8.9
In order to change the python version context switching without exporting environment variable. Please use the below video link to see:
https://youtu.be/jTN4MHNhJZs
After installing the newer version of python to your computer...
When you want to run a python program (e.g. 'program.py') from the terminal (using the latest version of python on your system); instead of running 'python program.py' run 'python3 program.py'
Similarly, if you want to use python in the terminal (using the latest version of python on your system) run 'python3' instead of 'python'
As a test try to run 'python3 --v' in the terminal...