I am trying to run a flask app in my command prompt on Windows 10. Whenever I try to run it, it errors out when trying to import torch. I have created a fresh environment and installed the latest pytorch from this page https://pytorch.org/get-started/locally/ but it still doesn't work in command prompt. Oddly, if I just execute code that says "import torch" in jupyter notebook or VS code, it doesn't error out. It only gives me the error in Command Prompt.
Does anyone know what the issue could be?
Short answer: If possible, use Anaconda Prompt. It's accessible from the Start Menu > Anaconda > Anaconda Prompt.
Long answer: The problem is due to the order of the different Python installations in the PATH variable, as you can see with echo %PATH%. System Python comes first, which can be seen with which python.
The recommendation is to use Anaconda Prompt, since it properly sets up the PATH variable with all required Anaconda paths. More importantly, it avoids interfering with other software in regular command prompt. So, your command prompt won't have any unintended consequences from using Anaconda.
If there is a reason that requires you to use regular command prompt, you can change the order with set PATH=C:\Anaconda\;%PATH% (temporary for this command prompt) or setx PATH=C:\Anaconda\;%PATH% (permanent). There are other libs that may be required to be included as well, which you can check by printing the PATH variable at the Anaconda Prompt.
Another option (Win10): Start Menu > type 'environment' > click 'Edit the system environment variables'. Click Advanced > Environment Variables... Double click Path and change the order between system Python and Anaconda Python.
I have installed python 3.3.2 and pygame 1.9.2a0. Whenever I try to import pygame by typing:
import pygame
I get following error message :
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pygame
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pygame
ImportError: No module named 'pygame'
>>>
I went through some of the questions related to this error but none of the solution helped.
I have 64 bit machine with Win7 OS
go to python/scripts folder, open a command window to this path, type the
following:
C:\python34\scripts> python -m pip install pygame
To test it, open python IDE and type
import pygame
print (pygame.ver)
It worked for me...
Here are instructions for users with the newer Python 3.5 (Google brought me here, I suspect other 3.5 users might end up here as well):
I just successfully installed Pygame 1.9.2a0-cp35 on Windows and it runs with Python 3.5.1.
Install Python, and remember the install location
Go here and download pygame-1.9.2a0-cp35-none-win32.whl
Move the downloaded .whl file to your python35/Scripts directory
Open a command prompt in the Scripts directory (Shift-Right click in the directory > Open a command window here)
Enter the command:
pip3 install pygame-1.9.2a0-cp35-none-win32.whl
If you get an error in the last step, try:
python -m pip install pygame-1.9.2a0-cp35-none-win32.whl
And that should do it. Tested as working on Windows 10 64bit.
I was trying to figure this out for at least an hour. And you're right the problem is that the installation files are all for 32 bit.
Luckily I found a link to the 64 pygame download! Here it is: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
Just pick the corresponding version according to your python version and it should work like magic. The installation feature will bring you to a bright-blue screen as the installation (at this point you know that the installation is correct for you.
Then go into the Python IDLE and type "import pygame" and you should not get any more errors.
Props go to #yuvi who shared the link with StackOverflow.
open the folder where your python is installed
open scripts folder
type cmd in the address bar. It opens a command prompt window in that location
type pip install pygame and press enter
it should download and install pygame module
now run your code. It works fine :-)
I had the same problem and discovered that Pygame doesn't work for Python3 at least on the Mac OS, but I also have Tython2 installed in my computer as you probably do too, so when I use Pygame, I switch the path so that it uses python2 instead of python3. I use Sublime Text as my text editor so I just go to
Tools > Build Systems > New Build System and enter the following:
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
}
instead of
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
}
in my case. And when I'm not using pygame, I simply change the path back so that I can use Python3.
The current PyGame release, 1.9.6 doesn't support Python 3.9. I fyou don't want to wait for PyGame 2.0, you have to use Python 3.8. Alternatively, you can install a developer version by explicitly specifying the version (2.0.0.dev20 is the latest release at the time of writing):
pip install pygame==2.0.0.dev20
or try to install a pre-release version by enabling the --pre option:
pip install pygame --pre
Resolved !
Here is an example
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame
try this in your command prompt:
python -m pip install pygame
I was getting the same error. It is because your version of Pygame is not compatible with your version of Python or Pydev. Go to this link and get the proper version of Pygame for your current version of Python. Ctrl F to find it faster or click on the word python in blue. up at the top. While you instal Pygame it should find the Python path by itself. At least mind did any ways. I run Pygame through Eclipse with Python 3.4.
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Since no answer stated this:
Make sure that, if you are using a virtual environment, you have activated it before trying to run the program.
If you don't really know if you are using a virtual environment or not, check with the other contributors of the project. Or maybe try to find a file with the name activate like this: find . -name activate.
Install and download pygame .whl file.
Move .whl file to your python35/Scripts
Go to cmd
Change directory to python scripts
Type:
pip install pygame
Here is an example:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame
I just encountered the same problem and found that I am having multiple interpreters of the different versions installed in my system and pygame got installed in one of them when I installed it using command but in my IDE another interpreter was selected so this messed up my system, try to see if you are also having the same situation.
Just use this command in the terminal python3 -m pip install -U pygame --user
I am a quite newbie to python and I was having same issue. (windows x64 os)
I have solved, doing below steps
I removed python (x64 version) and pygame
I have downloaded and installed python 2.6.6 x86: https://www.python.org/ftp/python/2.6.6/python-2.6.6.msi
I have downloaded and installed pygame (when installing, I have chosen the directory that I installed python): http://pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
Works well :)
You don't need 64 bit Python on Win64 system, just install the 32bit versions of both Python and Pygame and they will work just fine (and there is a ton more modules for them anyways).
I’m using the PyCharm IDE. I could get Pygame to work with IDLE but not with PyCharm. This video helped me install Pygame through PyCharm.
https://youtu.be/HJ9bTO5yYw0
(It seems that PyCharm only recognizes a package; if you use its GUI.)
However, there were a few slight differences for me; because I’m using Windows instead of a Mac.
My “preferences” menu is found in: File->Settings…
Then, in the next screen, I expanded my project menu, and clicked Project Interpreter. Then I clicked the green plus icon to the right to get to the Available Packages screen.
I ran into the error a few days ago! Thankfully, I found the answer.
You see, the problem is that pygame comes in a .whl (wheel) file/package. So, as a result, you have to pip install it.
Pip installing is a very tricky process, so please be careful. The steps are:-
Step1. Go to C:/Python (whatever version you are using)/Scripts. Scroll down. If you see a file named pip.exe, then that means that you are in the right folder. Copy the path.
Step2. In your computer, search for Environment Variables. You should see an option labeled 'Edit the System Environment Variables'. Click on it.
Step3. There, you should see a dialogue box appear. Click 'Environment Variables'. Click on 'Path'. Then, click 'New'. Paste the path that you copies earlier.
Step4. Click 'Ok'.
Step5. Shift + Right Click wherever your pygame is installed. Select 'Open Command Window Here' from the dropdown menu. Type in 'pip install py' then click tab and the full file name should fill in. Then, press Enter, and you're ready to go! Now you shouldn't get the error again!!!
First execute python3 then type the command import pygame,now you can see the output
For this you have to install pygame package from the cmd (on Windows) or from terminal (on mac). Just type pip install pygame
.If it doesn't work for you, then try using this statement pip3 install pygame .
If it is still showing an error then you don't have pip installed on your device and try installing pip first.
make sure if you are on windows that your library directory is added to path
This may happen when pygame didn't installed, install the pygame first
pip
pip install pygame
if dont work update the PIP by goto python install folder and type
python -m pip install --upgrade pip
hope it work
Try this solution:
Type in to cmd (Windows):
C:\Users\'Your name'> pip install -U pygame
You should remove python -m, py -m, python3 -m before the pip
Also remove --user behind it.
It will said:
C:\Users\viait>pip install -U pygame
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
Downloading pygame-2.1.2-cp310-cp310-win_amd64.whl (8.4 MB)
---------------------------------------- 8.4/8.4 MB 1.7 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.1.2
Then test it in your IDE or cmd:
(CMD example)
C:\Users\viait>python
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.1.2 (SDL 2.0.18, Python 3.10.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
(IDE example)
import pygame
You can do this without any errors.
You could use
pip install pygame
but if you use IDE like PyCharm, then you could just either install it from Python Packages or use right click at the package name then left click on Show Context Actions then left click on Install package pygame
(Personally, I recommended using Python Packages for the package installing because it has documentation with it)
You gotta use Pycharm and install it in Terminal using pip install pygame and also after that enter Pycharm and hover on pygame in the "Import pygame" and in Pycharm it will tell you to download that and you can easily download it and enjoy your result
I was having the same trouble and I did
pip install pygame
and that worked for me!
I know this similar question has been answered before but I made the mistake of installing python 2.7 after a Anaconda installation. Now when I use the pip function in cmd, it install the package in the python 2.7 folder and not the Anaconda folder. using python -m pip function in cmd I get:
c:/python27/python .exe -m pip
I just want to use the Anaconda directory instead of this.. any easy way to specify it?
thank you..
You have to change your PATH environment variable so that the Python 2.7 directories are removed. The easiest way is to uninstall this extra Python version. If you would like to edit the PATH directly, see here: https://superuser.com/a/284351/229278
To quote the answer for Windows:
Windows
GUI
Open Control Panel » System » Advanced » Environment Variables.
Type control sysdm.cpl,,3 in the Run dialog (+R) and click Environment Variables.
For editing user variables you can also type
%windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables
in the Run dialog.
Right-click (My) Computer and click on Properties, or simply press +Break.
In XP click on Advanced » Environment Variables.
In Vista+ click on Advanced system settings » Environment Variables.
There are many other ways of reaching the same place, such as by typing "environment variables" in the Start Menu/Screen search box and so on.
Environment variables in Windows are separated into user and machine/system specific values. You can view and edit their values there. Their current values upon launch are made available to all programs.
There is also Rapid Environment Editor, which helps setting and changing environment variables in Windows without the need to go deep into the system settings. Another open source program for Windows with which the path environment can be edited very conveniently is Path Editor.
So I'm trying python 2.7 on my Windows. It is running Windows 8. I cannot add it to my path. I've done the usual: using the advanced system settings, environment variables, adding C:\Python27 in system variables.
However, when I type Python in command prompt it says 'python is not recognized ..'
I think that the essence of this question is how to install Python and be able to use it from the command line. The steps below show how to get all that working. Check that you didn't miss anything:
From https://www.python.org/download/releases/2.7.6 download appropriate Python 2.7.6 Windows Installer. (If that link doesn't work, check https://www.python.org/downloads/)
Run the file
Select install for all users or install just for me, click Next
You'll see it installs under the C:\Python27 folder, click Next
Click Next again for the 'Customize Python' step
Click Finish
Open Control Panel, then System
Click 'Advanced system settings' on the left
Click the 'Environment Variables' button
Under 'System variables' click the variable called 'Path' then the 'Edit...' button. (This will set it for all users, you could instead choose to edit the User variables to just set python as a command prompt command for the current user)
Without deleting any other text, add C:\Python27; (include the semi-colon) to the beginning of the 'Variable value' and click OK.
Click OK on the 'Environment Variables' window.
Open a new command prompt window type python, you will have python running in the command prompt. Note: command prompt windows open prior to setting the Environment Variable will not have the python command available.
Easiest way is to open CMD or powershell as administrator and type
set PATH=%PATH%;C:\Python27
System variables usually require a restart to become effective. Does it still not work after a restart?
Make sure you don't put a space between the semi-colon and the new folder location that you are adding to the path.
For example it should look like...
{last path entry};C:\Python27;C:\Python27\Scripts;
...not...
{last path entry}; C:\Python27; C:\Python27\Scripts;
How to install Python / Pip on Windows Steps
Visit the official Python download page and grab the Windows installer for the latest version of Python 3.
python.org/downloads/
Run the installer. Be sure to check the option to add Python to your PATH while installing.
Open PowerShell as admin by right clicking on the PowerShell icon and selecting ‘Run as Admin’
To solve permission issues, run the following command:
Set-ExecutionPolicy Unrestricted
Next, set the system’s PATH variable to include directories that include Python components and packages we’ll add later. To do this:
C:\Python35-32;C:\Python35-32\Lib\site-packages\;C:\Python35-32\Scripts\
download the bootstrap scripts for easy_install and pip from https://bootstrap.pypa.io/
ez_setup.py
get-pip.py
Save both the files in Python Installed folder
Go to Python folder and run following:
Python ez_setup.py
Python get-pip.py
To create a Virtual Environment, use the following commands:
cd c:\python
pip install virtualenv
virtualenv test
.\test\Scripts\activate.ps1
pip install IPython
ipython3
Now You can install any Python package with pip
That’s it !!
happy coding
Visit This link for Easy steps of Installation python and pip in windows http://rajendralora.com/?p=183
Type this in Windows PowerShell or CMD:
"[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")"
After running the command, please restart PowerShell or CMD. If it still doesn't work, restart your PC.
there is a simple procedure to do it go to controlpanel->system and security ->system->advanced system settings->advanced->environment variables
then add new path enter this in your variable path and values
i'm using python 2.7 in win 8 too but no problem with that. maybe you need to reastart your computer like wclear said, or you can run python command line program that included in python installation folder, i think below IDLE program. hope it help.
GUI Option:
Open System Properties
a. Type it in the Start Menu
b. Use the keyboard shortcut Win+Pause)
c. From Windows Explorer address bar go to
%windir%\System32\SystemPropertiesProtection.exe
d. Write SystemPropertiesProtection in run window and press Enter
Switch to the Advanced tab
Click Environment Variables
Select PATH in the System variables section
Click Edit
Add python's path to the end of the list (the paths are separated by semicolons).
For example:
C:\Windows;C:\Windows\System32;C:\Python27
Command Line Option:
Run Command Prompt as administrator
Check existing paths under PATH variable (the paths are separated by semicolons). If your python folder already listed then no need to add again. Default python folder is C:\Python27
C:\Windows\system32>path or C:\Windows\system32>echo %PATH%
Append python path using setx command. The /M option sets the variable at SYSTEM scope.
The default behavior is to set it for the USER.
C:\Windows\system32>setx /M PATH "%PATH%;C:\Python27"