Can I safely remove Python 2.7.17

I am having some problems with some of my python scripts.

I have Python 2.7.17 which is the default.

I also have Python 3.10.7.

Can I safely remove Python 2.7.17 ?

A quick google yields

and

https://wiki.ubuntu.com/FocalFossa/ReleaseNotes#Python3_by_default

It was even faster than posting the question :slight_smile:

In general, your python script should also specify which major version it is for, which allows both to be installed without confusion.

Doing so uses the older python.

My script uses functions that need python 3.

Not sure what you refer to by "doing so."

Does your script have the shebang (first line) specifying the version of python you want it to run on?

If so, and the script is executable, then you should be able to run it by giving //<script.py>. Or just <script.py> if it is in a directory listed in your current environment's PATH. The other alternate is to say python3 <script.py>

This is what does not work in 18.04. It does work in 20.04.
PIL is installed.

#! /usr/bin/python3
import os
import sys
from PIL import Image

if len(sys.argv) > 1:
    if os.path.exists(sys.argv[1]):
        im = Image.open(sys.argv[1])
        target_name = sys.argv[1] + ".jpg"
        rgb_im = im.convert('RGB')
        rgb_im.save(target_name)
        print("Saved as " + target_name)
    else:
        print(sys.argv[1] + " not found")
else:
    print("Usage: convert2jpg.py <file>")

# Traceback (most recent call last):
  # File "/home/andy/Python/png2jpg.py", line 10, in <module>
    # from PIL import Image
# ModuleNotFoundError: No module named 'PIL'

The space after the shebang looks peculiar. It may be permissible, I'm not sure.

Does

$ which python3

yield

/usr/bin/python3

What if you start the script manually specifying the interpreter with the script as a parameter

/usr/bin/python3 /<path>/<to>/<script.py>

For instance, in my 18.04, "which python3" yields the expected "/usr/bin/python3" and "python3 -V" gives "python 3.6.9"

The problem in 18.04 is that python can not find the modules that the script references.

In 18.04 the modules are under my home directory.

In 20.04, those modules are in a different location.

It is a big mess. I prefer not having to boot to 20.04 to do my python work.

Google search "location of Python modules" The top six hits all give the same information about setting the PYTHONPATH environment variable, along with information about where the default location of modules is. Default may differ from one distribution to another.

Stating that the problem was with finding modules might have been better as the original question. Not sure how removing 2.7 could ever have helped the problem currently described.

Hope that will point you in the right direction.

2 Likes

I wonder if there is anyone on this forum using 18.04 who uses python?

Hi @fixit7,

how did you install Python and Python Imaging Library? Python3 version for UM 20.04 is 3.8.10. You claim you have 3.10.7. It is not in official repositories yet. What's the output from /usr/bin/python3 --version in your 18.04 and 20.04 installations?

I installed it from source code.

I am talking about 18.04 not 20.04 where it works fine.

/usr/bin/python3 --version
Python 3.6.9

I understand, that you have problems with 18.04. I don't have 18.04, however, your problem does not look like distro-related. You are referring to /usr/bin/python3 interpreter in your script, and it points to:

And it probably does not know about your installed imaging library package. What's your final goal? Just to run script? Run in using Python 3.10? Run specific imaging library version?

Since it does work in 20.04, its hard not to think it is distro related.

My goal is get all python scripts to work in 18.04.

I am getting feedback from several sites, but nothing has worked.

I may just have to accept that I will have to boot to 20.04 to develop/run any scripts that use modules.

I recommend you using your preinstalled Python 3 (you have version 3.6.9) and just install Pillow package using pip - Python package management tool.

  1. install pip for Python 3:
    sudo apt install python3-pip
  2. upgrade pip:
    python3 -m pip install --upgrade pip
  3. install Pillow:
    python3 -m pip install --upgrade Pillow

In theory, that's enough to run your script. For some reason, in 20.04 Python 3 has Pillow preinstalled (at least, I don't remember installing Pillow). That's a likely reason why your script runs in 20.04. You can now check preinstalled packages in 18.04 using python3 -m pip list command.

2 Likes

I will try your suggestions.

python3 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Requirement already satisfied: pip in /usr/local/lib/python3.10/site-packages (22.2.2)
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
WARNING: There was an error checking the latest version of pip.

You now have two different Python 3 versions installed.
Try running python3.6 -m pip install --upgrade pip and python3.6 -m pip install --upgrade Pillow .

The first command was successful.

python3.6 -m pip install --upgrade Pillow .
Defaulting to user installation because normal site-packages is not writeable
ERROR: Directory '.' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.

You don't need a trailing dot symbol.
python3.6 -m pip install --upgrade Pillow