Python cant recognize my installed modules [duplicate] - python

I currently have Python 3.10.5 and pygame 2.1.2.
VScode 1.70.2
Versions
For some reason there is the following warning: Image for warning
Import "pygame" could not be resolved PylancereportMissingImports
Even though I have the pygame library installed using "pip install pygame".
And when I run the file by pressing the "Run Python File" in the right top corner, it runs perfectly without an error. It even uses the terminal to show whatever I am printing.
My issue here is the warning and that intellisense for pygame isn't working.
What can I do to fix that.
I already tried to make a clean install on everything, python, pygame, even VScode. Nothing.

Try to reload vscode (by command pallete or just restart the app), reinstall pygame and make sure python path is selected. Also it is worth to trying selecting interpreter. Open command pallete and type: Python: Select Interpreter, then select your python version.

yes as the #NoBlockhit stated probably you have 2 different python versions installed and vs code choses the one without the pygame library (https://code.visualstudio.com/docs/python/environments) if you scrolldown it shows you how to chose an in interpreter...

Related

Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource)

whenever I try to import matplotlib or matplotlib.pyplot in VS Code I get the error in the title:
Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource)
or
Import "matplotlib.pyplot" could not be resolved from source Pylance(reportMissingModuleSource)
The hyperlink of the reportMissingModuleSource sends me to https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules, where it says:
"Diagnostics for imports that have no corresponding source file. This happens when a type stub is found, but the module source file was not found, indicating that the code may fail at runtime when using this execution environment. Type checking will be done using the type stub."
However, from the explanation I don't understand exactly what's wrong and what I should do to fix this, can someone help me with this?
I can reproduce your question when I select a python interpreter where doesn't exist matplotlib:
So, the solution is opening an integrated Terminal then run pip install matplotlib. After it's installed successfully, please reload window, then the warning should go away.
Just changes the interpreter to 2.7.x in left-bottom corner enter image description here
I experienced a similar issue even after installing vs_BuildTools.
Matplotlib could not be resolved from source parlance
What to do:
open the Command Palette (Ctrl+Shift+P)
Type: Python: Select interpreter Here is the interpreter that worked for me!
Navigate to your project.
Select the latest interpreter or check what interpreter
I had the same issue. Sometimes it's back to multiple versions of python on your device. You just need to change the path. Make sure the correct Python interpreter is selected in your IDE.
In vscode Press CTRL + Shift + P or (⌘ + Shift + P on macOS) to open the command palette. Then type Python select interpreter in the search field and choose the right version.
I have the same issue - I did two things and its working now
Check if there is some earlier versions of python installed on your machine - if yes then remove and reinstall the latest one
Second install the Microsoft C++ Build Tools
https://visualstudio.microsoft.com/visual-cpp-build-tools/
Restart the app and run again.
I was getting the same problem and realised the package install path for my active virtual env wasn't listed in sys.path
after appending this location to sys.path my .ipynb was able to import matplotlib

Installing Tkinter to use on Pycharm on a Mac

I just downloaded Pycharm because Spyder on Anaconda wasn't working anymore on my Mac. I am trying use Tkinter and have tried to run code using both
from tkinter import *
and
import tkinter as tk
The second is from my python class. Both times it returns
ImportError: No module named tkinter
I have tried to search for "tkinter" on Python Interpreter but there's no solid "tkinter." Only others like tkinter.math. Please help.
Tkinter would not be visible on Python Interpreter since it is a part of standard Python installation, the only additional modules or packages which user downloads are visible in Settings. Since, Spyder on your PC is not working as well, make sure python is installed properly and is configured correctly with path details properly set up.
I think you should check the interpreter of PyCharm.
Or Reinstall the python package and make a virtual environment.

How does one successfully download AND import pygame?

the problem:
as part of my studies, i have tried to download PyGame for my own independent project. I've currently downloaded 1.9.6, as well as currently owning IDLE version 3.8. i have imported it many times:
folder.
on one occasion , i placed it in its own separate folder with the coding file in another internal folder
when importing it into a new python script, the result was that there was "no module named Pygame", even though it was in a file containing the recent edition (this method was used successfully to download Tkinter).
I also tried making a python script outside the contained folder like this
this,too had the same result.
Any idea what exactly to do?
any more important information
The computer type is a MAC 10.13.6 and the IDE is IDLE. As previously mentioned, the downloaded version is version 1.9.6 and i have not a clue what how many bits my computer manages. Apologies that this is probably one of MILLIONS of the exact same question answered.I've tried at least 2 of these solutions.
First you need to make sure that you have pip installed. Type pip in your cmd (command prompt) and if you get no errors you are fine. If you have errors follow this link to get it. If you are stuck on that link - basically all you need to do is uninstall python and then when you are installing it again tick the box that says:
Add Python 3.5 to PATH
Now just finish installing python.
Next you need to install pygame with pip install pygame in your cmd - run it as administrator just to be safe. If you receive errors try updating pip with python -m pip install --upgrade pip. Now create and save a new python file. Import pygame in that file by using the code import pygame. The output should be:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Now you are free to code in pygame! I hope this helped.

PyGame not working in Eclipse using Python3.4

I am using Linux Mint 17 (Ubuntu 14.04) and already got pygame 1.9.2 working in Eclipse on my Windows PC. Now I also installed it on this machine, but it doesn't work. Thus there is no apt-get for python3-pygame I downloaded the source from https://bitbucket.org/pygame/pygame and built and installed it without getting errors.
When running the following on the command line I receive no error:
import pygame
pygame.init()
If I try the same in an Eclipse PyDev project I get the error: "Undefined variable from import: init".
First I thought that my Interpreter was not set up correctly, but the path where pygame is installed is added to the libraries (/usr/local/lib/python3.4/dist-packages). I realized that for Python2.7 pygame is installed in /usr/lib/pytho2.7/dist-packages instead, but this directory doesn't exist for python3.4 on my machine.
Since it seems to work on the command-line it must have something to do with the Eclipse or Pydev settings, right?
UPDATE:
Ok, now things are getting really confusing. I found out that pygame indeed is also working from within eclipse if I run the project, but only giving me these error messages for eg. pygame.init(), pygame.QUIT, pygame.K_ESCAPE, pygame.KEYDOWN. I find it very strange, because pygame.time.Clock() or pygame.display.set_caption() don't give error messages. So I only get undefined variable from import errors (also see here How do I fix PyDev "Undefined variable from import" errors? ).
Use sudo pip3 install pygame, pip3 should work assuming you have python3 and setuptools installed.
You can install pip3 using sudo apt-get install python3-pip and then use sudo pip3 install package_name to get whatever you want.
The only thing which worked for me (I spent two days now finding out what the problem is) is to add "pygame" to the forced builtins for the Interpreter (Window -> Preferences -> PyDev -> Interpreter -> Python Interpreter -> Forced Builtins Tab -> New -> pygame.
This is only a workaround, but at least I am rid of those errors for now and auto-completion still works. I didn't find any other solution that worked for me so far.
For me the problem was resolved by importing *.
instead of:
import pygame
try this:
from pygame import *
now instead of calling init() by saying pygame.init() just use init()
also you should not have to explicitly call using pygame. for the most part.
NOTE: after playing around with this in Eclipse I did end up having to use both:
import pygame
from pygame import *
for whatever reason one of my functions only works if I have pygame.even.get()
but the test of my code does not need to use pygame.
(I am sure there is a perfectly good reason but I am still very new to python)

pygame import error in eclipse

when I import pygame using eclipse on my mac, there's a import error:
However, I can still run this program correctly, So what can I do to fix it?
to quick fix you can do:
PyDev Package Explorer>right click your project>properties>PyDev - PYTHONPATH>External Libraries>Add source folder>PythonX.X(version where you installed pygame)/Lib/site-packages/pygame
this will include one external library for this project, but you should check if you are working with the right python version you installed.
I have 3 versions of python, pygame is only on Python2.6, so all my new projects that use pygame must have python2.6 syntax and interpreter python26.
To check your interpreter settings:
Window(menu)>Preferences>PyDev>Interpreter - Python>New>interpreter name you can put anything you may remember, interpreter executable find the python.exe and put the path there, OK.
Now you have added the entry for the interpreter, if you look down in the same window, into Libraries you can add many packages you want the interpreter to look for to resolve names and syntax.
They make Pygame for every version of Python. I have it for Python 3.4 it came out in March 2014. If you are getting the error there is no module named Pygame. It is because Pygame is not in Python's path or you have the wrong version of Pygame for your current version of Python. You can find more Pygame downloads here that arnt on pygame.org. Click on the blue word Pygame at the top and it will take you to the proper section of the page.
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Download the one you need for your Python version. If you hold the mouse over it it should show more info about the version in a tiny pop up. I am on windows but it found the path by itself. I'm not sure if it will for mac.

Categories