Score:0

Python package works on the command line but cannot be found in a script

us flag

Basically from the title and the image below, I have installed pyserial and it works in the Python terminal but not in a script.

I have done pip3 install pyserial, and have also tried pip install pyserial and python3 -m pip install pyserial, which return that I already have the package installed.

When I use python3, enter the Python terminal, and enter import serial there are no errors. When I have a script using import pyserial and run it using python3 ./serial_test.py, it gives the error.

Why does one throw an error and the other not? And how may I fix this?

Here is a screenshot of my terminal:

Image of my terminal

My python script, no matter if the script uses the serial library, throws the error:

python script

Edit 1: One thing I forgot to mention was that when I do pip3 --version it shows:

pip 22.0.3 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)

instead of:

/usr/lib/python3...

as listed here, although this still would not explain the difference in the in-terminal code.

Maybe a solution would be to install it using pip for /usr/lib/python3, however I am unsure how to do that.

Edit 2: As I look further into this, I will update my findings. When I do whereis python3, I get:

python3: /usr/bin/python3 /usr/bin/python3.8 /usr/bin/python3.8-config /usr/lib/python3 /usr/lib/python3.8 /usr/lib/python3.9 /etc/python3 /etc/python3.8 /usr/local/lib/python3.8 /usr/include/python3.8 /usr/share/python3 /usr/share/man/man1/python3.1.gz

I am unsure how programs work on the command line, so I am not sure why there are multiple places for python3, but maybe it will explain the issue.

Edit 3: which python3 shows:

/usr/bin/python3

and which pip3 shows:

/usr/local/bin/pip3
Score:1
cn flag

Option A:

You can try to run:

/usr/local/bin/pip3 install serial

and then:

/usr/bin/python3 serial_test.py

and check if you still face the same problem. If you do, then try Option B.

Option B:

You can try to create a virtual environment via:

virtualenv -p python3 venv

or:

python3-venv

and try to execute the program.

NOTE

You are using Python 3 as your user in normal mode, while you are using root when launching the script, as shown in the image you uploaded. Using Python 3 as sudo will launch python as the root user and maybe the $PATH will not be the same for python and pip.

You might have installed the serial module in your user's pip path, but it may not be present in root user's pip path and that's why you are facing the issue!

SmokeySpace avatar
us flag
Okay it was definitely your last point that it was because I was running it as sudo, running "sudo python3" and then importing the library throws the error as well. You definitely got me on the right track to fixing the issue so thanks! Do you know how I could either install the library for sudo or give python or my user more permissions? (not sure how it works)
Amogh Saxena - REXTER avatar
cn flag
It was pleasure helping you :) Sure you can use ```sudo python3``` and ```sudo pip3 install <xyz>``` to install modules for root. But I won't recommend it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.