Installing jupyter with Python 3.8 fails with "SyntaxError: invalid syntax" because it's using Python 2.7

Viewed 1326

On a mac, I've installed Python 3.8 and I am trying to install jupyter using:

pip install jupyter

It runs, but I get the following output in the terminal:

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 11, in <module>
    load_entry_point('pip==21.0.1', 'console_scripts', 'pip')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2843, in load_entry_point
    return ep.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2434, in load
    return self.resolve()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/Library/Python/2.7/site-packages/pip-21.0.1-py2.7.egg/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

Seems odd as I am on Python 3.8 and the output seems to refer to Python 2.7.

Anyway, when I then try

jupyter notebook

Still, the command jupyter is not found.

1 Answers

From the error

File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve  
   module = __import__(self.module_name, fromlist=['__name__'], level=0)  
File "/Library/Python/2.7/site-packages/pip-21.0.1-py2.7.egg/pip/_internal/cli/main.py", line 60  
   sys.stderr.write(f"ERROR: {exc}")

you can see that the pip you used is from the Python 2.7 installation. The latest pip 21.0 will not work on that version, and f-strings are only available starting Python 3.6.

The installation failed by the way, so trying to use jupyter command after that would, of course, still not work.

Seems odd as I am on Python 3.8 and output seems to refer to Python 2.7

You will always have to explicitly indicate which Python version you want to use. Since macOS comes with a built-in Python 2.7 installation, you'll have to explicitly indicate the Python 3 specific one, usually python3 and pip3.

Depending on how you installed your Python 3.8, best practices are:

  1. Using the <python> -m pip command, which means "use the pip that comes from this <python> installation". (You can check the version with python -V)

    ~$ python3 -V
    Python 3.9.1
    
    ~$ python3 -m pip install jupyter
    
  2. Creating a virtual environment and installing your packages inside the virtual environment

    ~$ python3 -V
    Python 3.9.1
    
    ~$ python3 -m venv myenv
    
    ~$ source ./myenv/bin/activate
    (myenv) ~$ python -V
    Python 3.9.1
    (myenv) ~$ python3 -V
    Python 3.9.1
    
    (myenv) ~$ pip install jupyter
    

    The virtual environment takes care of using the correct Python versions and pip install-ing things also uses the correct versions. Compared to option 1 which installs things system-wide, the packages you installed in a virtual environment will only be available when you activate the virtual environment.

Related