I recently formatted my laptop and installed the Ubuntu operating system. I did not explicitly install Python. To check if it is pre-installed, I ran python --version in the terminal and got this:
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
Then I checked python3 --version and got this: Python 3.6.9.
How is this possible? Should I install python as well? Or can I continue to use python3? Would this cause any problems?
Recent Ubuntu versions do not install python 2 by default, as python 2 is now "dead". However, the command python is not (yet) linked to python 3, to avoid some confusion; traditionally, python was for python 2 while python3 was for python 3.
In case you want to change this behavior and use python to run python 3, try installing a package named 'python-is-python3'. Then you can run python --version and see that it points to python 3.
In theory it might cause confusion for some legacy programs, but I haven't seen any issue so far for about a year.
In 20.04 LTS, the python included in the base system is Python 3.8. Python 2.7 has been moved to universe and is not included by default in any new installs.
Remaining packages in Ubuntu which require Python 2.7 have been updated to use /usr/bin/python2 as their interpreter, and /usr/bin/python is not present by default on any new installs. On systems upgraded from previous releases, /usr/bin/python will continue to point to python2 for compatibility. Users who require /usr/bin/python for compatibility on newly-installed systems are encouraged to install the python-is-python3 package, for a /usr/bin/python pointing to python3 instead.
— https://wiki.ubuntu.com/FocalFossa/ReleaseNotes
Just python on Linux traditionally refers to Python 2. python3 is the correct command if you want to use Python 3 (which you should, as Python 2 is EOL).
TL;DR: Just use python3
Python on Ubuntu refers to Python v2. If you were to run
python --version
you would see an output in the format of
Python 2.x.y
Python 2 has been sunset, which means it no longer will be updated and the recommendation is to move to Python3.
To run a program with Python 3, you just need to use python3 command instead.
python3 ./app.py
Related
I am new to Python and Linux env, so a little confused.
I want to find where my Python3.9 is installed, so that I can update the PATH..
Running python --version or python3 --version give me Python 3.6.8
I want to install Python 3.9, so when I run sudo dnf install -y python39 it gives me
Package python39-3.9.12-1ep1.el8.x86_64 is already installed.
ls /usr/bin/python* doesn't show 3.9
Output of above command - /usr/bin/python /usr/bin/python3 /usr/bin/python3.6
/usr/bin/python3.6m
Also tried alternatives --config python but it shows only 3.6
-----------------------------------------------
* 1 /usr/libexec/no-python
+ 2 /usr/bin/python3
which python3.6 gives /usr/bin/python3.6
which python3.9 gives /usr/bin/which: no python3.9 in....
rpm -ql python39-3.9.12-1ep1.el8.x86_64 gives me
/etc/gdbinit.d/python39.gdb. /opt/python3.9.
/opt/python3.9/bin
I could be missing something obvious, if someone can point me in the right direction it will be awesome.
what happens when you enter the following command: python3 --version and is it any different compared to running python --version?
I am not sure what is your actual requirement is. But if you have already installed python 3.9 in your machine, then you can have this and all previous versions applied to different projects. This is selected when you create virtual environments. For example if you want to run a project that built on 3.6, then you can select python interpreter 3.6 at the time of creation.
So the virtual machine I was working was built using vagrant, it had python 3.6 in usr/bin and python 3.9 in opt/python3.9. Newbie like me who wanna understand what /opt is - https://www.baeldung.com/linux/opt-directory
To search any package used command - rpm -ql python39-3.9.12-1ep1.el8.x86_64
Once package is found add it to path as symlinking binaries can be confusing
vim ~/.bashrc
PATH=/opt/python3.9/bin
I am trying to get python 3 working on my OSX laptop.
I need to install requests for python 3, and it isn't working.
I think I've managed to get pip installed for both python 2.7 & python 3 however...
Whenever I use 'pip' it points to python2... I can't seem to access the pip for python 3?
In all likelihood, pip3 will be installed pointing to your Python 3 installation, so your use case is probably solvable by just switching from:
$ pip install foo
to:
$ pip3 install foo # Or pip3.7 install foo if you need to disambiguate further
That said, it can get kind of complicated when you have many different Python installs, where pip/pip3 might have been installed pointing to a Python version that doesn't correspond to the python/python3 you're using, which can be quite confusing.
If you know python & python3 are the correct executable, just use it to invoke pip on your behalf. It's fairly easy too, just check your version to be sure it's the one you expect (e.g. on my system):
$ python --version
Python 2.7.15rc1
$ python3 --version
Python 3.6.6
then use the appropriate one with -mpip, a flag to run an installed module/package via the chosen Python as the "main" executable, bypassing the need for specifically versioned pip executable entirely. So if you wanted to install foo for Python 3.6 on my machine, you'd run:
$ python3 -mpip install foo
This is especially useful on Windows, where the pip executables often either don't exist, or are not installed in the PATH, so it's irritating to use them. Instead, use the Windows launcher that comes with any modern Python 3 version (but manages all Python versions on the machine), and is used to disambiguate among various versions. For example:
C:\>; Installs foo for latest installed version of Python 3
C:\>py -3 -mpip install foo
C:\>; Installs foo for latest installed version of Python 2
C:\>py -2 -mpip install foo
C:\>; Installs foo for latest installed version of Python 3.6
C:\>py -3.6 -mpip install foo
Essentially, any use of pip can be replaced by executing the Python interpreter directly with the -mpip option to run the pip package as the "main" executable.
This trick applies to many other tools with dedicated launchers that are often not installed in the PATH, particularly on Windows, and it makes updates easier too; my Windows shortcut for launching ipython3 never used a hardcoded path to the launcher (e.g. C:\Program Files\Python36\Scripts\ipython3.exe), instead using %WINDIR%\py.exe -3 -mIPython. In addition to being more portable (the shortcut "just works" on any Windows system with a semi-recent Python 3 install), it's self-updating; when I upgraded from 3.6 to 3.7, the shortcut didn't have to change at all (I had to run py -3 -mpip install ipython again to get IPython reinstalled, but once I'd done that, the shortcut seamlessly began referring to the 3.7 install with no changes needed).
Run this command to find the python that is used before running pip: which python. You can do the same idea to find which pip version is being run: which pip
You’ll need to create separate virtual environments in order to use different python versions and/or python dependencies. Use something like conda or venv to do this. Then, ensure that the desired python version virtual environment is activated prior to installing a new package with pip.
To install requests for python3, use pip3 install requests which is the pip installer for Python 3 modules.
This guide has some further info on getting Python 3 working on a mac.
https://docs.python-guide.org/starting/install3/osx/
try to sudo apt-get update first then sudo apt-get install python3-pip --fix-missing
I recently started using elementary OS. It's based on Ubuntu.
During installation it installs python 3.6.
I installed python 3.8 manually by installing the following packages: python3.8, python3.8-dev, python3.8-minimal, python3.8-venv.
I also updated the link to the python binary with:
sudo ln -sf /usr/bin/python3.8 /usr/bin/python3
After this a couple of things stopped working.
For example when I tried to execute a non-existing command it didn't print the error message that it cannot found the command, but it displayed a python stack trace. That one I solved with:
cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
sudo ln -s apt_inst.cpython-36m-x86_64-linux-gnu.so apt_inst.so
As you can see the python error was because it couldn't find the compiled binaries for the apt module.
So this issue was solved, but there are a couple of similar ones, and none of them can be solved this way, as the module binary is not compatible with python 3.8.
Is it possible to remove python 3.6 completely and override it with 3.8 so that the module binaries also get updated? Or can python 3.8 and 3.6 coexist? I would be fine with the /usr/bin/python3 link pointing to python 3.6 and I would manually execute /usr/bin/python3.8 or create a different alias or link for it. However when I print out the sys.path with /usr/bin/python3.8 I get this:
/usr/lib/python38.zip
/usr/lib/python3.8
/usr/lib/python3.8/lib-dynload
/home/{username}/.local/lib/python3.8/site-packages
/usr/local/lib/python3.8/dist-packages
/usr/lib/python3/dist-packages
The trouble is with the last one. That's where the modules are with the 3.6 compatible so files. Can I somehow force python 3.8 to completely ignore the last module search path (without always stating sys.path.remove in my scripts) ?
I recommend you undo your symlink overwriting your systemwide version of Python 3. As you have found, replacing the python3 executable might create some problems, as it is used under the hood.
In general, you should leave your system python[3] installation alone for this reason and it is common to use virtual environments. This can be done as follows (assuming you are in some project directory):
python3.8 -m venv venv
source venv/bin/activate
The first command will create a virtual environment ('venv') in the directory venv. The second command will 'activate': now python (and in this case, python3 and python3.8) all refer to your original python3.8 in the context of this shell. You will have to repeat this if you launch a new shell.
This will also allow you to install packages using pip without cluttering your system installation. The use of virtual environments and pip is an incredibly common workflow in the Python development world.
In terms of shell and 'global' Python management, you can also use pyenv to manage your Python versions and what is available in the shell. pyenv is quite nice if you want to run a particular version of python, say 3.8.0 but not 3.8.1.
I am using a Python on a Mac and I know that Python 2 comes preinstalled on the system (and in fact usable through Terminal). Is there a way to make it so Terminal can run Python 3? Can/should you set this as a default? I know changing the default settings for Python version usage could break your system so should I just install Python 3 and then use it through its launch icon instead?
Use can use python version management tool
https://github.com/yyuu/pyenv
Package manager:
pip3 install something
Interpreter:
python3
You can use python 3 in terminal after you have python 3 installed. Every time you run a command type python3 instead of python if you are using pip to install modules you can do this:
But you must have pip installed for python 3
$ python3 -m pip install something
Best option is to install Python through Anaconda. This allows easy management and much more. You can have virtual environments having different Python versions as well as different modules installed.
as usual in Mac python 2.7 is already installed, however if you installed python 3+
then you can just type in terminal: python3
so that you can use the newer version that you installed.
if you want to use python 2.7 then just type: python
I have Python 3.4 installed on my Linux computer.
sudo apt-get install python3.4
However, when I run python -V, it shows that Python 2.7.6 is being used.
How do I tell the system to use the updated version of Python?
The answer to this question for a windows computer is at How to update version of Python?, but I couldn't find an answer for Linux.
On Linux, installations of python3 installed by the package manager (e.g. apt) can be called as python3. You might need to specify the version - e.g. python3.5 if the package manager has installed more than one, or you've compiled your own installations from source.
you can specify the version in the shebang.
write #!/usr/bin/env python3.
when run via ./my_script.py it will run in python3.
otherwise run it via python3 my_script.py.
if you just want to start an interactive python shell start it with python3
you also can be more specific with the version. just replace python3 with python3.4 (if installed)
In ubuntu various python executable are places under /usr/bin/ and might look like
/usr/bin/python
/usr/bin/python3.2
/usr/bin/python3.4
etc. so when you execute a command python -v it looks for a file with that name in that location. so to choose your version specify it like python3.4 -v
you can replace the simlink /usr/lib/python with /usr/bin/python3.4 to make that "default"