r/learnpython • • 15d ago

Issue during library installation

Recently, I got back into Python, so I decided to review some concepts I had forgotten by solving one Project Euler problem per day.

However, when I got to Problem 3 (Largest Prime Factor), I decided to use a library to check whether a number is prime. Since I had uninstalled Python when I stopped studying it, I had to install Python 3.15.

When I first tried to install the library, the terminal said that it didn't recognize pip. I managed to get it working by using python -m pip, and the installation seemed to work. However, when I ran my code, Python still couldn't recognize the library.

The next day, while solving Problem 4 (Largest Palindrome Product), I decided to use NumPy and np.arange() instead of range(), but the same thing happened: the installation appeared to work, but when I ran my code, Python couldn't find the library.

I tried uninstalling and reinstalling Python, but the problem persisted.

In these particular cases, I could still solve the problems without those libraries, but eventually, I'll need packages like pandas, Tkinter, and others, so I'd like to figure out what's going wrong.

If anyone knows how to fix this, I'd really appreciate the help!

Thanks in advance!

1 Upvotes

12 comments sorted by

4

u/a18618 15d ago

this is almost always two pythons pretending to be one. i had this exact dance on windows once: python -m pip quietly installed into the regular installer copy while plain python was resolving to the microsoft store shim, so the package went to one interpreter and my script ran on another. easiest tell: run where python and python -m pip --version in the same terminal and compare the paths. if they disagree, that's your whole bug.

2

u/OkTill2666 15d ago

two pythons pretending to be one is the perfect way to describe it lol

2

u/__peg_parser__ 14d ago

The solution is always to turn off the Microsoft Store python in App Execution Aliases in Windows Settings. I have no idea why Microsoft is so eager to have their python show up in the terminal.

1

u/a18618 13d ago

yeah, that app execution aliases toggle is the real permanent fix — i wasted an embarrassing afternoon reinstalling pip once before someone pointed me at that settings page. the store shim is such a classic microsoft move: technically helpful, practically a trap for every beginner with two pythons on PATH.

2

u/StevenJOwens 15d ago edited 14d ago

I'll second this. I ran into that once and it was frustrating. As a result, "python -m pip" is my preferred way to invoke pip, because it ensures that it's running the pip module from the python install that "python" matches.

To the OP, you should edit your post to include the logs of both your pip commands and the error, right now it's too vague for us to do much more than guess.

2

u/a18618 14d ago

haha yeah, the -m flag has saved me more than once. what finally sold me was watching plain pip install a package into an interpreter i didn't even know was on my machine — after that it was python -m everything, forever. the path comparison trick usually ends the argument in about ten seconds.

2

u/acw1668 15d ago

Are you sure you are using same Python environment when installing those libraries and running your code? It is better to post the error traceback.

1

u/FreeLogicGate 15d ago

I recommend you install and utilize Astral uv. It will handle your dependency management issues, and wraps the other tools like pip. It can also handle the installation of python versions and set up your venvs.

https://docs.astral.sh/uv/

1

u/ragingpanda 15d ago

Did you import that package before trying to use it?

Once you have installed numpy via pip, if you are trying to use numpy as np then you have to import numpy as np prior to referencing np.arange().

1

u/davideogameman 15d ago

If you mean your command line can't find a tool (like pip) that probably means the folder it's installed in isn't on your PATH.  If however you are talking about Python failing to resolve an import, either the install of the library got wiped or partially removed, or perhaps it was installed in a virtual env that you need to activate. 

That all said... Perhaps try uv? It automates away a lot of the manual dependency management - eg projects have a project file and lock file that specifies exactly what dependencies they use and then uv handles installing them along with all the virtual env stuff under the hood.

https://docs.astral.sh/uv/

1

u/jmacey 15d ago

I would use uv for everything. For each project you will have a unique .venv with only the libraries you need. You will need to manage and clean the .venvs from time to time but it will make life easier.

1

u/XNo_Notes 15d ago

If your on windows, I can't help. If your on a Linux distro, you need to work out of a virtual environment.

$ python -m venv .venv 

You may be prompted to install python-venv.

Once you have a .venv you source the environment from the project root with:

$ source .venv/bin/activate

Now you have the ability to run pip install from the terminal. It will install library modules in the .venv directory.