Python saying that it cant find pygame - python

At the top of my python code, I put import pygame as usual, and have installed the module on my computer. So why do i get this when running my code? I have already tried deleting pygame and reinstalling it and it has not worked. The files are on my computer. Can anyone help me?
Traceback (most recent call last):
File "C:\Users\<myuser>\Documents\RPG\main.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'

I think you might not have added the python path when you installed python. If you're using windows, search for Edit the system environment variables. Click on Environment variables, in the system variables section, double click on path -- and add the path of your python software.
ex: C:\Program Files\Python310\Scripts\ & C:\Program Files\Python310*

Related

Issues making Python modules to work inside Ubuntu environment at PyCharm

I am having issues with properly installing Tkinter and PyQt5 in my Ubuntu environment (I have a Windows computer with Ubuntu activated and linked to PyCharm).
I have tried installing via the packages installer inside PyCharm and via codes in the Ubuntu terminal itself (like apt-get install and pip install). Even though some installations seemed to work, the modules are not being loaded properly. In fact, Tkinter doesn't even show up as a download option inside PyCharm (I only found "tk" but it doesn't seem to work the same).
After trying a bunch of stuff, these are the messages I get when trying to load these packages:
Traceback (most recent call last):
File "part3_displaying_table.py", line 18, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
ImportError: bad magic number in 'PyQt5': b'\x03\xf3\r\n'
Traceback (most recent call last):
File "part2_visualizing_venn.py", line 24, in <module>
from PIL import Image, ImageTk
File "/usr/local/lib/python3.6/dist-packages/PIL/ImageTk.py", line 28, in <module>
import tkinter
ModuleNotFoundError: No module named 'tkinter'
Any ideas of what might be happening?
P.S. 1: I have the exact same repositories installed in my Windows OS and importing the modules/running the code work completely fine.
P.S. 2: I need to install those in the Linux environment because I will need to call a program in my code that is not compatible with Windows; as a consequence I will have to run the whole code in my Ubuntu environment.
From https://wiki.python.org/moin/TkInter:
Step 2 - can Tkinter be imported?
Try the correct command for your version at the Python prompt:
>>> import Tkinter # no underscore, uppercase 'T' for versions prior to V3.0
>>> import tkinter # no underscore, lowercase 't' for V3.0 and later
If it works, go to step 3.
If it fails with "No module named Tkinter", your Python configuration needs to be changed to include the directory that contains Tkinter.py in its default module search path. You have probably forgotten to define TKPATH in the Modules/Setup file. A temporary workaround would be to find that directory and add it to your PYTHONPATH environment variable. It is the subdirectory named lib-tk of the Python library directory (when using Python 1.4 or before, it is named tkinter).

How do I fix python 3.8.5. error "ImportError: cannot import name 'open_code' from 'io' (unknown location)"

I'm a complete novice as far as python is concerned, but need to install it for PlatformIO. I installed python-3.8.5-amd64.exe on Windows 10 and installed PlatformIO in VSCode. But when I try to do a build I get:
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\io.py", line 54, in <module>
ImportError: cannot import name 'open_code' from 'io' (unknown location)
Since io.py is installed as part of the python installation, I assume the 'open_code' symbol should be installed. Running the Repair option from python-3.8.5-amd64.exe and rebooting claims everything's fine, but it still fails.
Can someone explain, in simple terms, what I need to do to fix this?
Note: there's an answer here which indicates it's due to there having been 2 versions of python installed and that something's pointing at the wrong verison, but it doesn't explain how to fix it in any way that I understand (as well as being for a Linux installation).
Thanks.
The solution was to delete the .platformio folder from my user folder. Obviously, PlatformIO was 'remembering' the previous python install and confusing things!
this happened to me when multiple versions of Python was installed , and the PATH environment variable had a python path whose version was different from what i was executing. Adjusted the path variable to demote the unused python version and promote the currently used version and it solved the problem.thanks!

Messed up Python directories

I'm a complete beginner to coding, Python, and anything beyond user-level computing. I've been learning to code with Python and everything was fine until I tried to import a module outside of Python's Standard Library (pyglet).
pip install pyglet worked and now returns:
> Requirement already satisfied: pyglet in
c:\users\nick\appdata\local\programs\python\python38-32 (1.5.7)
Then I received an error message when running the module in PyCharm.
>C:\Users\Nick\venv\Scripts\python.exe C:/Users/Nick/PycharmProjects/learning1/leaning1.GUI.py
Traceback (most recent call last):
File "C:/Users/Nick/PycharmProjects/learning1/leaning1.GUI.py", line 1, in <module>
import pyglet
ModuleNotFoundError: No module named 'pyglet'
Process finished with exit code 1
I think there are a few possible sources for the issue.
I think I might be saving files in multiple directories, so PyCharm is unable to find what I need. If that's the problem please direct me to a resource on learning how to organize files on my computer (something very basic about directories because I know nothing).
I installed python via python.org, but then Microsoft Store was being opened automatically when I tried to check my python version in the command line. I also downloaded Python from Windows Store.
I tried to solve my problems by completely uninstalling PyCharm, Python (windows store), and Python (python.org). I also tried to delete all previous .py files. I don't think I located everything.
I messed with the interpreter settings in PyCharm.
What I really want is to just start over from a clean slate by scrubbing all Python things!
There are a few steps you should take to prevent this sort of issue:
Use a virtual environment: steps to create a virtual environment
Verify that PyCharm's project interpreter is the virtual environment you just created (in my example, the project is named cheetah and the virtual environment is also named cheetah):

Modules working in Pycharm but not Shell or Command Line

I'm new to Python and having trouble with several modules. In Pycharm, everything works fine. I am able to import modules, pip installer works fine. However, for some reason I'm running into issues when trying to import modules in the Python Shell, or when running programs using the command line.
For example, I can import Beautiful soup just fine in pycharm, but when I try in the shell I get:
Traceback (most recent call last):
File "", line 1, in
import bs4
ModuleNotFoundError: No module named 'bs4'
Similarly, when I save a program in pycharm and try to run it from the command line, I get:
C:\Users\Tony>hellowness.py
Traceback (most recent call last):
File "C:\Users\Tony\MyPythonScripts\Hellowness.py", line 3, in
import selenium, bs4, pyperclip
ModuleNotFoundError: No module named 'bs4'
The correct directory is being used, because I can run simple programs that don't use modules without problems in the command line.
Sorry if this has been answered eleswhere. I searched the forum but people seem to being having trouble installing modules. The modules are installed fine (I think) they just don't work in the Python Shell or the command line. How do I go about troubleshooting this?
Thank you,
Tony
I finally figured it out. As you said there were two "Site Packages" directories installed. One for Pycharm and one for Python itself. I am sure there's a more efficient way to solve it, but for now I just copied any relevant modules from Pycharm's site packages folder over to Python's.
Thanks!

Cannot import module with PyCharm, why?

PyCharm 2018.1.3 pro / Python 3.6.1 / Module: neo4j-driver 1.5.3
The strange thing is that if I trying to import neo4j.v1 using script editor in PyCharm and run it I'm getting error:
Traceback (most recent call last): File "C:/experiment/Cypher.py",
line 1, in from neo4j.v1 import GraphDatabase ModuleNotFoundError: No
module named 'neo4j'
Process finished with exit code 1
But this script works just fine if I call it using Windows PowerShell environment using simple command prompt:
> python C:/experiment/Cypher.py
So, I think that something is wrong with settings or with PyCharm. What should I do to solve this problem?
From pycharm,
goto settings -> project Interpreter
Click on + button on top right corner and you will get pop-up window of Available packages. Then search for neo4j python package.
Then click on Install package to install the package.
neo4j.v1 is not a default python module, it is not automatically installed when you install python.
To install neo4j.v1 Follow the instructions here.
Its possible to install it directly from pycharm, as other answers have pointed out, but its better to download from the official source to make sure all related dependencies and packages are properly installed.

Categories