Fixing common pip and python package issues

This article is WIP

im not done writing or improving this. Expect typos, weird grammar and general badness.

Introduction

Im going to outline a few common issues with pip, python versions and python packages. Im not going to recommend any specific tool, but rather show a good way of managing things yourself.

Topics:

How to check what version im using?

To check the Python version, use python -V.
To check if you're running 32 or 64 but python, use python -V -V. It should tell you the exact version, including something like 64 bit (AMD64).

To check the pip version, use pip -V.
This should tell you something like pip 20.2.3 from c:\python39\lib\site-packages\pip (python 3.9). The important part here is the last bit, python 3.9. Make sure this matches your python version.

Heres some resources on how to check in different IDEs or Editors:

Help, i cant import the package i just installed!

So, you're trying to import a package but it just wont work. You just get a ModuleNotFoundError.

Lets start with the obvious question: Did you actually install the package? Normally, packages are installed with pip install packagename.

Another reason this may fail is a mismatch in the version. Follow the steps in the How to check what version im using section and make sure the python version of pip matches the python version you're using, whether you're using commandline to run your code or a IDE. If you want to make sure you're installing the packages in the correct python version, use python -m pip install packagename, this makes sure pip is using the same version that the python command is referring to.

There are also some packages that are installed under a different name than the one used for importing. For example, discord.py, a popular Python framework for making discord bots, is installed with pip install discord.py, but imported as import discord. In a similar vein, packages that have a - in the name are often imported with a _ instead of a -, like flask-sqlalchemy becomes flask_sqlalchemy.

How do i run my python scripts with the version i want?

There are multiple ways you can specify the version, some are dependent on your Operating System (Windows, Linux)

On Windows: By default, python installations on Windows come with whats called the "pyLauncher", which makes it easy to run any of the installed versions.
The pylauncher is used with py which should refer to the same version as the python command by default. Other versions can be accessed by passing the version like this py -3.7.

a few examples:

  • py my_file.py runs the python file with the primary python version
  • py -3.8 my_file.py runs python with the specified version (3.8)
  • py -3.8 -m pip install requests runs pip for the specified version (3.8) and installs the requests package
  • py --list lists all avaliable versions

On Linux: When installing multiple python versions in most linux distros, their binaries are automatically on the PATH as python3.8, python3.9 etc. This makes it easy to install and use multiple versions. In many distributions, python refers to Python 2, whereas python3 refers to the primary version of Python 3

a few examples:

  • python3 my_file.py runs the python file with the primary python version
  • python3.8 my_file.py runs python with the specified version (3.8)
  • python3.8 -m pip install requests runs pip for the specified version (3.8) and installs the requests package
  • ls -ls /usr/bin/python* lists all avaliable versions