Mac Pycharm Interpreter not working - python

I can't run my python programs in pycharm. I installed python3, pycharm and anaconda. I can open the project and write python code, but can't run the program in pycharm. I can use the terminal to run my program and it works fine. Any ideas how to use the right interpreter? Where can I find it? BTW I work with a mac.
When I try to add an interpreter I can add an Virtualenv Environment, Conda Environment or System Interpreter. When I click on System interpreter and choose one, it wont work. I tried to find my python version with the terminal and use it in pycharm, but it wont do anything.
Thats what I see:
entire screen
The icon also seems weird (red cross):
weird icon
Greetings, Stefan

Inspect your conda python installation via:
$ conda info
Mine is: /root/miniconda, so basic grepping shows what pythons do I have:
root#d03107c14a07:/notebook# ls -al /root/miniconda/bin/ | grep python
-rwxrwxr-x 1 root root 129 Apr 16 16:20 ipython
-rwxrwxr-x 1 root root 129 Apr 16 16:20 ipython2
lrwxrwxrwx 1 root root 9 Apr 16 16:20 python -> python2.7
-rwxrwxr-x 1 root root 1689 Apr 16 16:20 python-config
lrwxrwxrwx 1 root root 9 Apr 16 16:20 python2 -> python2.7
-rwxrwxr-x 1 root root 10302 Dec 21 2016 python2.7
And running it like below shows it works:
root#d03107c14a07:/notebook# /root/miniconda/bin/python
Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>>
We have the path, for me it's: /root/miniconda/bin/python
So, as we have it localized, we need to set it up with our Pycharm.
Go to Settings/Preferences and into Project interpreter, choose a settings wheel and Add - On the left you've got three options, choose Conda, your env might be found automatically, but if it isn't then you have the path to paste ;)
Official docs ref
conda info ref

Related

Python, Django Celery job interface upgrade to ubuntu 20.04 celery jobs not running

Very New to Celery/Django
Over the weekend, we upgraded the ubuntu server to 20.04, and while the web interface is up and running and I can see the defined jobs when I attempt to run any job the job does not run.
I am not seeing any errors in the logs (assuming I am looking at the correct apache logs).
Everything is down and I am not sure where to debug celery. If I could find out where the jobs are located then I would try to run using the celery CLI.
Updated from ubuntu 16.04.
It is using mod_wsgi not using a virtual env that I can tell. I believe it is using the system python version 2.7.
I've tried restarting.
Any help/pointers are much appreciated.
The 2.7 python did stick around, probably as you suggested due to the upgrade.
It looks to be the default in the path:
/home/barrow/bin:/home/barrow/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
barrow#server:~$ which python
/usr/bin/python
barrow#server:~$ python
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
There are, of course, other versions of python sitting around.
-rwxr-xr-x 1 root root 5490352 Jun 2 04:49 /usr/bin/python3.8
lrwxrwxrwx 1 root root 33 Jun 2 04:49 /usr/bin/python3.8-config -> x86_64-linux-gnu-python3.8-config
-rwxr-xr-x 1 root root 3674216 Mar 8 06:02 /usr/bin/python2.7
lrwxrwxrwx 1 root root 33 Mar 8 06:02 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root root 33 Feb 20 16:02 /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config
lrwxrwxrwx 1 root root 34 Feb 20 16:02 /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x 2 root root 4723808 Feb 20 16:02 /usr/bin/python3.6
-rwxr-xr-x 2 root root 4723808 Feb 20 16:02 /usr/bin/python3.6m
lrwxrwxrwx 1 root root 7 Apr 15 2020 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 14 Apr 15 2020 /usr/bin/python-config -> python2-config
lrwxrwxrwx 1 root root 9 Mar 13 2020 /usr/bin/python2 -> python2.7
lrwxrwxrwx 1 root root 16 Mar 13 2020 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 9 Mar 13 2020 /usr/bin/python3 -> python3.8
lrwxrwxrwx 1 root root 16 Mar 13 2020 /usr/bin/python3-config -> python3.8-config
I am able to run the celery command in the CLI, so I removed that post here. I am "searching" for where the job might be. I can't wait until I understand this.
My thoughts are that I might be able to run the job using celery on the command line. Or worst case that would help with the debugging and eliminate is as a culprit.

How to align python version on Debian

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)

jupyter not using version set by pyenv

So this all worked perfect on a different machine. Have had to get it to work on my desktop. pyenv is installed there with different versions I just added miniconda3-latest which displays nicely in the prompt (there is a .python-version file in the directory below) Then ran pip install pymysql after that prompt. When I run python now at that prompt it imports fine:
(miniconda3-latest) cardamom#pegasus ~/Desktop/Project $ python
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
>>>
...but if I launch jupyter notebook
(miniconda3-latest) cardamom#pegasus ~/Desktop/Project $ jupyter notebook
[I 13:38:28.714 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found
[I 13:38:29.216 NotebookApp] The port 8888 is already in use, trying another port.
[I 13:38:29.224 NotebookApp] [nb_conda] enabled
[I 13:38:29.278 NotebookApp] [nb_anacondacloud] enabled
[I 13:38:29.337 NotebookApp] ✓ nbpresent HTML export ENABLED
[W 13:38:29.337 NotebookApp] ✗ nbpresent PDF export DISABLED: No module named 'nbbrowserpdf'
[I 13:38:29.340 NotebookApp] Serving notebooks from local directory: /home/cardamom/Desktop/Project
[I 13:38:29.340 NotebookApp] 0 active kernels
[I 13:38:29.341 NotebookApp] The Jupyter Notebook is running at: http://localhost:8889/
[I 13:38:29.341 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
and try to import I get
ImportError Traceback (most recent call last)
<ipython-input-1-a4103d9b2333> in <module>()
----> 1 import pymysql
ImportError: No module named 'pymysql'
As I said, it works perfectly on a different machine.. Does anyone know how to get jupyter notebook, once launched, to correctly pick up the version which pyenv is reading from its .python-version file?
I found this gist which is pretty simple:
#!/bin/sh
if [ "$PYENV_VERSION" -ne "" ]
then
name=`pyenv version-name`
python=`pyenv which python`
else
name=`basename "$VIRTUAL_ENV"`
python="$VIRTUALENV/bin/python"
fi
jupyterdir=$(jupyter --data-dir)
kerneldir="${jupyterdir}/kernels/${name}"
echo "Installing jupyter kernel file $name for $python to $kerneldir ..."
pip install ipykernel
mkdir -p "$kerneldir"
cat > "$kerneldir"/kernel.json <<EOF
{
"argv": [ "$python", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "$name",
"language": "python"
}
EOF
cat "$kerneldir"/kernel.json
Once I understood what was happening there, I went ahead and installed the pyenv plugin pyenv-jupyter-kernel and I like it very much.
Step 3 from this blog was pretty informative, though I didn't follow the steps. I read them and understood another perspective on that gist. It's always good to take in a few opinions before you blindly copy-pasta. There are a lot of outdated instructions out there!
In the mean time I have 90% solved it. From this article on the topic I found this pyenv which command had never tried before. So can see that there are two things that can be called.
(miniconda3-latest) cardamom#pegasus ~/Desktop/Project $ which jupyter
/home/cardamom/.pyenv/shims/jupyter
(miniconda3-latest) cardamom#pegasus ~/Desktop/Project $ pyenv which jupyter
/home/cardamom/.pyenv/versions/miniconda3-latest/bin/jupyter
Jupyter notebook seems to call the first one but if I enter the second path with the word 'notebook' after if, it launches fine and there is only one kernel available being the one with my pymysql module in it:
import sys
print (sys.version)
3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
Just need to work out how to call that directly without the whole path..
Update 15.05.17
Well, I fixed it, or helped it to fix itself -
rm -rf /home/cardamom/.pyenv/shims/jupyter*
Then closed and relaunched the terminal.
Surely wasn't the cleanest way, hope it hasn't broken something else, but now at least just running jupyter notebook is launching a notebook which contains the kernel active in my directory miniconda3-latest. The two which commands as per above are still returning the same thing, but now if I list the jupyter things in the first directory:
(miniconda3-latest) cardamom#pegasus ~/Desktop/Project $ ll /home/cardamom/.pyenv/shims/jupyter*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-bundlerextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-console*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-kernelspec*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-migrate*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-nbconvert*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-nbextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-notebook*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-qtconsole*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-run*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-serverextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-troubleshoot*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-trust*
(miniconda3-latest) cardamom#pegasus ~/Desktop/Project $
...you can see that all those files just recreated themselves just a few minutes ago.
Kindly check what python version Notebook kernel is using, Most of the time like this error will come because of library installed for python3 and the running notebook kernel version may be Python2.
Check whether a specific version of python is specified in .local/share/jupyter/kernels/python3/kernel.json. Deleting this file helped me to have jupyter-notebook use the python version being set with pyenv.

RHEL6: Switch Python back to original default version

I am using RHEL6 which came with Python 2.6.6 pre-installed on it as the default python executable.
Some months ago, I installed Python 2.7 on it and switched the default python version to 2.7 (yes, idiot move I now realize). So now when I type python it runs Python 2.7.
Also: which python gives me /usr/local/bin/python
As you might expect, and I only found out recently, this has caused issues when trying to run some system scripts which depend on the version being 2.6. The 2.6 installation still exists and I can run it by either python2 or python2.6.
Also: which python2 gives /usr/bin/python2 and which python2.6 gives /usr/bin/python2.6
The problem is that I do not remember how I switched the default version to 2.7. I know I didn't create any alias, because I can't find any in the ~/.bashrc file.
Does anyone have any advice on how I can revert back by changes so that Python 2.6.6 becomes the default once again? I can provide any further information that might be necessary to analyze this problem. Moreover, I have a couple of other people around me who are also using RHEL6 and haven't played around with their python installations, so if I need to copy any original scripts from /usr/bin/ to get this fixed I can get it from them, as long as I know what to get.
Thanks in advance for any advice!
Responses to Barun Sharma:
Running ls -l /usr/local/bin/python* gives me:
-rwxr-xr-x. 2 root root 6111394 Jan 16 2015 /usr/local/bin/python
-rwxr-xr-x. 2 root root 6111394 Jan 16 2015 /usr/local/bin/python2.7
-rwxr-xr-x. 1 root root 1624 Jan 16 2015 /usr/local/bin/python2.7-config
lrwxrwxrwx. 1 root root 9 Dec 26 2014 /usr/local/bin/python3 -> python3.4
-rwxr-xr-x. 2 root root 8777236 Dec 26 2014 /usr/local/bin/python3.4
lrwxrwxrwx. 1 root root 17 Dec 26 2014 /usr/local/bin/python3.4-config -> python3.4m-config
-rwxr-xr-x. 2 root root 8777236 Dec 26 2014 /usr/local/bin/python3.4m
-rwxr-xr-x. 1 root root 3013 Dec 26 2014 /usr/local/bin/python3.4m-config
lrwxrwxrwx. 1 root root 16 Dec 26 2014 /usr/local/bin/python3-config -> python3.4-config
lrwxrwxrwx. 1 root root 16 Jan 16 2015 /usr/local/bin/python-config -> python2.7-config
create a soft link:-
ln -s /usr/bin/python2 /usr/local/bin/python
Or directly point /usr/local/bin/python to your python2.6 binary(i.e where ~/usr/bin/python2` points).
Follow these steps:-
1) rm /usr/local/bin/python. This would remove the soft link. But python 2.7 will still be there. You can check /usr/local/bin/python2.7.
2) ln -s /usr/local/bin/python2.6 /usr/local/bin/python. Symbolic link from python to python2.6
Also when you are working on python, I would advise you to use virtual environment and install your required python version and other packages. I generally do not install packages to system python when it is required for a specific project(rather do it on a virtual env).
Once you have your system back to normal, the way to add new versions of Python 2.7, 3.3, or 3.4 to RHEL (6 or 7) is to utilize Software Collections (aka RHSCL or SCL) which install along side the original Python versions. There are many other languages, databases, web servers, and other tools, too. RHSCLs are included in most RHEL subscriptions.
To get started: http://developers.redhat.com/products/softwarecollections/overview/
You can also find information here: https://access.redhat.com/products/Red_Hat_Enterprise_Linux/Developer/#rhscl=&dev-page=5
OR here: http://developerblog.redhat.com/tag/software-collections/

How to change default Python version?

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...

Categories

Resources