Error occurred while importing pennylane on Colab

Hi everyone, an error occurred while importing pennylane on Google Colab. I’ve tried many things, but the error still occurs.

Installation of Python 3.8.20

!apt-get update
!apt-get install python3.8 python3.8-distutils
!update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
!wget https://bootstrap.pypa.io/get-pip.py
!python3.8 get-pip.py

Installation of Packages

!python3.8 -m pip install --upgrade pip
!python3.8 -m pip install tensorflow==2.4.0
!python3.8 -m pip install --force-reinstall pennylane==0.17.0
!python3.8 -m pip install --force-reinstall strawberryfields==0.18.0
!python3.8 -m pip install pennylane-sf==0.16.0
!python3.8 -m pip install numba==0.53.1
!python3.8 -m pip install numpy==1.19.5 matplotlib scikit-learn pandas scikit-image
pip list
import pennylane as qml

If you run it, this error will occur.

I saw someone else resolve a similar issue with ‘pip install autoray==0.2.5’, so I added

!python3.8 -m pip install --force-reinstall autoray==0.2.5 

at the last of the ‘Installation of Packages’ cell.
But the problem wasn’t resolved.

Does anyone have an idea how to solve this problem?

I think you complicated it too much. This worked for me


So I just do pip install pennylane, without installing python3.8 inside Colab

1 Like

Welcome to the Forum @BEOMJOONG_KIM !

And thanks for jumping in to help @mchau :raising_hands:
It’s great to see everyone helping each other.

What works for me is !pip install pennylane (with the exclamation mark). That should be it!

Also, note that at the moment PennyLane is supported for Python v3.10-3.12, so Python 3.8 might not even work.

I hope this helps!

1 Like

Thank you for replying, @mchau and @CatalinaAlbornoz
I solved the problem by using

%%writefile cv_qnn.py
~~ # importing pennylane and other packages
~~ # other codes

and using

!python3.8 cv_qnn.py

Thank you!

1 Like