简体   繁体   中英

PIP will only install packages to Python 2

I have a computer running Ubuntu 16.04 that I am trying to use Python with. I need to install the pyserial package. Running pip install pyserial works with no problems, but when I try to import the package in python 3 (3.9.6) i get an error that there is no module named serial. When I try to run python3 -m pip install pyserial I get an error message /usr/local/bin/python3: No module named pip . I've run sudo apt-get install -y python3-pip with no errors but that doesn't resolve the issue with pip. Same with running pip3 install pyserial instead of just pip . I recently replaced python 3.5.2 with python 3.9.6, and when I do python3 -v I get a very long error message (posted below), so I assume that would have something to do with it, but I just am not sure. I don't know if the install got messed up and there are some things to clean up or if there is something else going on.

import _frozen_importlib # frozen
import _imp # builtin
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import '_warnings' # <class '_frozen_importlib.BuiltinImporter'>
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
import '_io' # <class '_frozen_importlib.BuiltinImporter'>
import 'marshal' # <class '_frozen_importlib.BuiltinImporter'>
import 'posix' # <class '_frozen_importlib.BuiltinImporter'>
import '_frozen_importlib_external' # <class '_frozen_importlib.FrozenImporter'>
# installing zipimport hook
import 'time' # <class '_frozen_importlib.BuiltinImporter'>
import 'zipimport' # <class '_frozen_importlib.FrozenImporter'>
# installed zipimport hook
# /usr/local/lib/python3.9/encodings/__pycache__/__init__.cpython-39.pyc matches /usr/local/lib/python3.9/encodings/__init__.py
# code object from '/usr/local/lib/python3.9/encodings/__pycache__/__init__.cpython-39.pyc'
# /usr/local/lib/python3.9/__pycache__/codecs.cpython-39.pyc matches /usr/local/lib/python3.9/codecs.py
# code object from '/usr/local/lib/python3.9/__pycache__/codecs.cpython-39.pyc'
import '_codecs' # <class '_frozen_importlib.BuiltinImporter'>
import 'codecs' # <_frozen_importlib_external.SourceFileLoader object at 0x7f41506b8700>
# /usr/local/lib/python3.9/encodings/__pycache__/aliases.cpython-39.pyc matches /usr/local/lib/python3.9/encodings/aliases.py
# code object from '/usr/local/lib/python3.9/encodings/__pycache__/aliases.cpython-39.pyc'
import 'encodings.aliases' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150653ca0>
import 'encodings' # <_frozen_importlib_external.SourceFileLoader object at 0x7f41506b8460>
# /usr/local/lib/python3.9/encodings/__pycache__/utf_8.cpython-39.pyc matches /usr/local/lib/python3.9/encodings/utf_8.py
# code object from '/usr/local/lib/python3.9/encodings/__pycache__/utf_8.cpython-39.pyc'
import 'encodings.utf_8' # <_frozen_importlib_external.SourceFileLoader object at 0x7f41506b8940>
import '_signal' # <class '_frozen_importlib.BuiltinImporter'>
# /usr/local/lib/python3.9/encodings/__pycache__/latin_1.cpython-39.pyc matches /usr/local/lib/python3.9/encodings/latin_1.py
# code object from '/usr/local/lib/python3.9/encodings/__pycache__/latin_1.cpython-39.pyc'
import 'encodings.latin_1' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150653d90>
# /usr/local/lib/python3.9/__pycache__/io.cpython-39.pyc matches /usr/local/lib/python3.9/io.py
# code object from '/usr/local/lib/python3.9/__pycache__/io.cpython-39.pyc'
# /usr/local/lib/python3.9/__pycache__/abc.cpython-39.pyc matches /usr/local/lib/python3.9/abc.py
# code object from '/usr/local/lib/python3.9/__pycache__/abc.cpython-39.pyc'
import '_abc' # <class '_frozen_importlib.BuiltinImporter'>
import 'abc' # <_frozen_importlib_external.SourceFileLoader object at 0x7f415066b280>
import 'io' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150653f70>
# /usr/local/lib/python3.9/__pycache__/site.cpython-39.pyc matches /usr/local/lib/python3.9/site.py
# code object from '/usr/local/lib/python3.9/__pycache__/site.cpython-39.pyc'
# /usr/local/lib/python3.9/__pycache__/os.cpython-39.pyc matches /usr/local/lib/python3.9/os.py
# code object from '/usr/local/lib/python3.9/__pycache__/os.cpython-39.pyc'
# /usr/local/lib/python3.9/__pycache__/stat.cpython-39.pyc matches /usr/local/lib/python3.9/stat.py
# code object from '/usr/local/lib/python3.9/__pycache__/stat.cpython-39.pyc'
import '_stat' # <class '_frozen_importlib.BuiltinImporter'>
import 'stat' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150608af0>
# /usr/local/lib/python3.9/__pycache__/_collections_abc.cpython-39.pyc matches /usr/local/lib/python3.9/_collections_abc.py
# code object from '/usr/local/lib/python3.9/__pycache__/_collections_abc.cpython-39.pyc'
import '_collections_abc' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150608b80>
# /usr/local/lib/python3.9/__pycache__/posixpath.cpython-39.pyc matches /usr/local/lib/python3.9/posixpath.py
# code object from '/usr/local/lib/python3.9/__pycache__/posixpath.cpython-39.pyc'
# /usr/local/lib/python3.9/__pycache__/genericpath.cpython-39.pyc matches /usr/local/lib/python3.9/genericpath.py
# code object from '/usr/local/lib/python3.9/__pycache__/genericpath.cpython-39.pyc'
import 'genericpath' # <_frozen_importlib_external.SourceFileLoader object at 0x7f41506401f0>
import 'posixpath' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150608ac0>
import 'os' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150680220>
# /usr/local/lib/python3.9/__pycache__/_sitebuiltins.cpython-39.pyc matches /usr/local/lib/python3.9/_sitebuiltins.py
# code object from '/usr/local/lib/python3.9/__pycache__/_sitebuiltins.cpython-39.pyc'
import '_sitebuiltins' # <_frozen_importlib_external.SourceFileLoader object at 0x7f4150680af0>
import 'site' # <_frozen_importlib_external.SourceFileLoader object at 0x7f415066ba60>
Python 3.9.6 (default, Jul 23 2021, 14:48:54) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
import 'atexit' # <class '_frozen_importlib.BuiltinImporter'>

Try to be explicit when intalling the package.Just mention the Python version you want to install with it and then intall it. Try: python3.9 -m pip install library_name You are facing this issue cause you have multiple python3.x installations so when you try python3 -m pip install library_name it might be installing the package with the other python3.x installation.

You can check the default python3.x version by typing in python3 --version and later on change the command python in your .bashrc file if you don't wanna specify it explicitly each time.

I have the same output when i try python3.8 -v on my system.But explicitly mentioning the verison name helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM