Module Imports Not Displaying

Hello! First time using the forum… I am a beginner with python and I recently started having an issue with modules not showing on Visual Studios. I keep getting the “ModuleNotFoundError: No module named ‘requests’”. As far as I am aware, I have the most up to date version of python and VS. I have tried so many video guides attempting to fix the problem but nothing has worked. When I check the command prompt it shows the modules are downloaded. Is there a specific file location for something that is not getting read? I’m super stuck!

If a package is not native to Python, in the command prompt, try:

pip install package_name

in your case:

pip install requests

It is downloaded already. it shows as a downloaded module with

pip list

Ok.

I see that it is shown under thePython 3.12 version.

Can you please add the following lines for verification purposes at the top of your script; before your importing lines:

import sys

major, minor, micro = sys.version_info[:3]
print(f"Your Python version is {major}.{minor}.{micro}")

What does that give you?

As another test, can you create a folder on your Desktop named: testDirectory and add your module named api.py here (to circumvent the OneDrive directory).

C:/Users/tanne/Desktop/testDirectory/api.py

That returns “Your Python version is 3.12.4”

This is also what I get when returning the pip version in command

pip --version

PS C:\Users\tanne> pip --version
pip 24.1.2 from C:\Users\tanne\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip (python 3.12)

It says “python 3.12”. Since it is not 3.12.4 do you think this could be the problem?

No, I don’t think so.

Can you please try running your script from the Desktop as per the 2nd recommended test.

Looks like it is still not working on that 2nd test
c:\Users\tanne\Desktop\testDirectory\api.py

Ok.

To make sure that the location of the requests package is within your PATH directories, compare the following:

  1. On your command line prompt, type:
pip show requests

In Python, type the following (from inside your api.py module / file and comment out: import requests):

import sys

for num, path in enumerate(sys.path):
    print(f'{num} {path}')

If the path result from the pip command does not match any of the paths from the sys.path, this I think is the issue.

Can you compare please.

We recommend that you use the py command to run python on Windows.
We also recomment that you use py -m pip to run pip on Windows.

By using py and py -m pip you make sure that the same version of python is used to install packages and to use them.

That is what @onePythonUser is thinking is the issue, python and pip are not matched.

1 Like

This is from the pip show requests command:

    PS C:\Users\tanne> pip show requests
    Name: requests
    Version: 2.32.3
    Summary: Python HTTP for Humans.
    Home-page: https://requests.readthedocs.io
    Author: Kenneth Reitz
    Author-email: me@kennethreitz.org
    License: Apache-2.0
    Location: C:\Users\tanne\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages
    Requires: certifi, charset-normalizer, idna, urllib3
    Required-by:

And this is from running api.py:

    0 c:\Users\tanne\Desktop\testDirectory
    1 c:\Users\tanne\AppData\Local\Programs\Python\Python312\python312.zip
    2 c:\Users\tanne\AppData\Local\Programs\Python\Python312\DLLs
    3 c:\Users\tanne\AppData\Local\Programs\Python\Python312\Lib
    4 c:\Users\tanne\AppData\Local\Programs\Python\Python312
    5 c:\Users\tanne\AppData\Local\Programs\Python\Python312\Lib\site-packages

Also thank you for your help and patience. This stuff is very and confusing new to me!

See how the requests location directory does not match any of your PATH directories. This is the confirmed issue. I have never used Visual Studios so I am not familiar with it. However, what you can do is to add the requests directory to your current PATH so that it can search this location as well.

import sys
   sys.path.append(r'C:\Users\tanne\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages') # add system path

Note that the PATH directories are where your program looks when importing packages. If your package is not within these paths, you will not be able to import it.

Yup, I totally understand. It was for me as well. :wink:

NOTE
One more thing to consider. A good way to verify if your file is working as expected is to use the IDLE IDE editor. I use this as a simple sanity check. If you open IDLE and open your api.py from there and run it, it should work with no issue (assuming the package has been downloaded to any of the default PATH directories of course).

It worked! Man this had been driving me crazy. I will look on the Visual Studios website to see how I can change the PATH directory as default. But this fix did make it work. Again, big thanks!

1 Like

By the way, I have my system set up such that my IDLE editor PATHs are where ALL of my packages are downloaded to when using pip. I then set up PyCharm to search my IDLE PATHs when importing packages. This is so that I don’t have multiple third party editor specific PATHs to consider.

Look to see if you can set up Visual Studio similarly. So, if you have multiple third party editors installed on your computer, they will all search packages from the same PATH.

1 Like