How to properly install pygame module in windows? And make games - python-2.7

I have written codes but it's showing cannot import pygame module

Download pygame from here: http://www.pygame.org/download.shtml
If you are not sure which version of python you are running, execute the following code in your shell:
import sysconfig
version = sysconfig.get_python_version()
print(version) #Python3
print version #Python2
Usually, Python3 will also accept the print command without brackets, so using sysconfig is the safest way. Sysconfig will also tell you which minor version you are using (such as 3.4.5)
Once you have download pygame, tryimport pygame in your shell. If this does not work, type help('modules'). This will now list all your modules. If pygame is not in there, check in which folder all other modules are saved and move the pygame module to that file path.
You can also use sysconfig to display all the file paths python is using:
import sysconfig
paths = sysconfig.get_paths()
print(paths) #For Python2, see print method above
I don't quite understand what you mean with "And make games", but the pygame website has a great documentary and some example codes to help you learn progamming with the pygame module.
Hope I could help,
Narusan

Related

ImportError: No module named vaderSentiment

I'm trying to run a code in python2.7 on windows os that uses sentiment analysis
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
and I'm getting this error
ImportError: No module named vaderSentiment
Can anyone help me with this?
Assuming you solved this one as it's from 7 months ago, but for anyone else searching for it:
Go into terminal/cmd and paste the following:
pip install vaderSentiment
More info on VADER: https://github.com/cjhutto/vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
#note: depending on how you installed (e.g., using source code download versus pip install), you may need to import like this:
#from vaderSentiment import SentimentIntensityAnalyzer
read the comment in a code
Try running your file with Python3 instead of just python. Sometimes when you have different pips/pythons installed on your computer you might have vaderSentiment installed in python2 when you need to run it in python3.

Importing Numpy in embedded Python c++ application

I would like to have a script invoke numpy from a c++ embedded python runtime by setting the runtime path to know about the numpy module located within site-packages.
However I get the error:
cannot import name 'multiarray'
from \Lib\site-packages\numpy\core__init_.py on the line
from . import multiarrray
I have tried to set the os.path to be xxx\numpy\core but it still cannot seem to find the multiarray.pyd file during the import statement
I have read through similar questions posed but none of the answers seem relevant to my case.
I am using Python 3.4.4 (32 bit) and have installed Numpy 1.11.1 using the wheel
numpy-1.11.1-cp34-none-win32.whl
python -m pip install numpy-1.11.1-cp34-none-win32.whl
Completed without any errors.
Seems like the failure message maybe more general than just an incomplete PYTHONPATH?
Also think it might be broader than Numpy in that ANY .pyd based package that is imported from the embedded environment will have this problem?
Any help appreciated.
Did you ensure all your NumPy includes: \numpy\core\include\numpy\ were present during the build? That's the only time I get those types of errors was if the build couldn't find all the NumPy includes... although during embedding I found that the numpy entire directory (already built on your build machine) has to be inside a directory under Py_SetPath(python35.lib;importlibs); assuming importlibs is a directory with NumPy inside and anything else you want to bundle.
Seems like the answer was to install python 3.4.1 to match the python34.dll version of 3.4.1.

No module named kivy.app

So I thought I'd toy around and try and learn Kivy, as it looks interesting. I have just started trying to get one of their examples working:
from kivy.app import App
from kivy.uix.widget import Widget
class MyPaintWidget(Widget):
pass
class MyPaintApp(App):
def build(self):
return MyPaintWidget()
if __name__ == '__main__':
MyPaintApp().run()
I get the following error:
C:\Kivy-1.8.0-py2.7-win32>python paint.py
Traceback (most recent call last):
File "paint.py", line 1, in <module>
from kivy.app import App
ImportError: No module named kivy.app
I have installed the latest version of Kivy. I see "app.py in the C:\Kivy-1.8.0-py2.7-win32\kivy\kivy folder.
Also, here is my PYTHONPATH:
>>> import sys
>>> for n in sys.path:
... print n
...
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\setuptools-2.0.1-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\pywin32-218-py2.7-win32.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\plyer-1.1.2-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\kivy_garden-0.1.1-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\requests-2.2.1-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\python27.zip
C:\Kivy-1.8.0-py2.7-win32\Python27\DLLs
C:\Kivy-1.8.0-py2.7-win32\Python27\lib
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\plat-win
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\lib-tk
C:\Kivy-1.8.0-py2.7-win32\Python27
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages
Any help would be greatly appreciated. Thank you.
I ran into this error message when I named the script kivy.py, because python looks first in the current directory to fill the dependency, so it never sees the real kivy package.
Renaming the script fixed it for me.
The name of the folder or file cannot be kivy or kivy.py. You need to rename it to another name and it will run ok.
So, I figured it out....I was not being very smart. I'm new to Kivy and I'll answer this for anyone else that is as green as I am. You can't just run this as a python program, doh.
Follow instructions [here] (http://kivy.org/docs/installation/installation-windows.html#start-a-kivy-application) and all will be right with the world.
If you installed kivy using pip you need to add the packages to your environment. In Windows Command Prompt do
pip show kivy
~Take note of the Location of the module.
Next you can do 1 of two things.
Configure any python files you write to refer to the location of your module with sys, use this tutorial for more info: https://kivy.org/docs/guide/environment.html
Add the location you found above to your PATH (Windows): https://www.computerhope.com/issues/ch000549.htm
2 Worked for me.
I was also facing the same issue while running sample app.
I followed simple steps given in : https://kivy.org/doc/stable/gettingstarted/installation.html#install-pip
It worked for me.
Create the virtual environment
Activate the virtual environment.
Install Kivy
run your .py file
I know I am too late but I hope this will help others, I just did not run it from the command prompt, I opened the python code in my python shell and then pressed run 'F5' and it worked for me.
If you're using a virtual environment (venv), make sure you're targetting the the correct Python interpreter. See here for how to to change to the venv interpreter in IntelliJ

No module named os.path : wrong Python being called by bash

OS: CentOS 6.6
Python 2.7
So, I've (re)installed Canopy after it suddenly stopped working after an abrupt shutdown. It worked fine immediately after the install (I installed as my default Python). But after one reboot, when I try to open it with /root/Canopy/canopy (the icon under applications no longer works, either), I get the following error:
(Canopy 64bit) [xxuser#xxlinux ~]$ /root/Canopy/canopy Traceback (most recent call last): File "/home/xxuser/qiime_software/sphinx-1.0.4-release/lib/python2.7/site-packages/site.py", line 73, in <module>
__boot() File "/home/xxuser/qiime_software/sphinx-1.0.4-release/lib/python2.7/site-packages/site.py", line 2, in __boot
import sys, imp, os, os.path ImportError: No module named path
I found this link: Python - os.path doesn't exist: AttributeError: 'module' object has no attribute 'path', but both of my os.py and os.pyc were 250 and 700 bytes, respectively. There was another file called site.py which was 0 bytes and site.pyc was about 100 bytes. What are these files? And would deleting them hurt anything (which is what they did)? And why is this happening after reboot? (using reboot command).
I also found this: https://groups.google.com/forum/#!topic/spyderlib/hKB15JYyLqM , which could be relevant. I've updated my python path before with sys.path.append('/..')
My guess is that for some reason os.path isn't in sys.path? and __boot can't find it? But I'm new to Python and Linux and want to know what I'm doing before I go modifying any boot files, paths, etc.
Thanks in advance.
More information (saw that I'm supposed to update new info in an edit to original question. New to this.)
From one of the comments:
This is what I got:
import os.path
import posixpath
os.path
module 'posixpath' from '/home/xxuser/qiime_software/python-2.7.3-release/lib/python2.7/posixpath.pyc'
posixpath
module 'posixpath' from '/home/xxuser/qiime_software/python-2.7.3-release/lib/python2.7/posixpath.pyc'
Looks like os.path is there.
Could this have to do with a permissions error? I have it installed to /root/Canopy/canopy and I found this: docs.python.org/2/library/os.html#module-os (section 15.1.4). Does that make sense?
I'm also not sure if the following is related, but it might possibly. I can no longer seem to update my path with sys.path.append('/file/path/here'). It works until I close the terminal, then I have to re-append the next time I want to call a module from the new directory. Are sys.path and os.path related in any way?
Just fixed this on OSX with:
brew uninstall python
brew install python
No idea why, never seen it in 5 years of working with Python :S
It turns out that I was onto something with my last comment.
I'd downloaded a bunch of biology modules that depend on python, and so many of them came with their own install. When I added the modules to ~/.bashrc, my bash began calling them in advance of my original CentOS install. Resetting ~/.bashrc and restarting (for some reason source ~/.bashrc didn't work) eliminated all of the extra stuff I'd added to my $PATH and Canopy began working again. I'm going to go through and remove the extra installations of python and hopefully the issue will be behind me. Thanks to everyone who posted answers, especially A.J., because that's what got me thinking about .bashrc .
Edit: With some better understanding, this was all because of using python in a virtual environment. Canopy was resetting my path every time I opened it. I'm using a self-installed virtual environment now and have configured my path.
Try seeing if you have posixpath by typing import posixpath:
>>> import os.path
>>> os.path
<module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> ^D
bash-3.2$ python
>>> import posixpath
>>> posixpath
<module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>

py2exe can't find msvcp90.dll

I'm working on converting a simple GUI script written with Python 2.7 and Pyqt4 into a standalone executable using py2exe. I keep getting "no such file exists" errors, and I've managed to fix a few, though this one seems stubborn. It can't find msvcp90.dll, and returns an error message with a short traceback to distutils and then back to my py2exe script, which isn't very enlightening.
I've installed the MS C++ redistributable runtime, as recommended in
py2exe fails to generate an executable
but my script still can't locate the .dll. Below is my py2exe script, with the name of my script blocked out:
from distutils.core import setup
from py2exe.build_exe import py2exe
import sys, os, zmq
sys.argv.append('py2exe')
os.environ["PATH"] = \
os.environ["PATH"] + \
os.path.pathsep + os.path.split(zmq.__file__)[0]
setup(
options = {'py2exe':{'bundle_files':1,"includes":["zmq.utils",
"zmq.utils.jsonapi","zmq.utils.strtypes"]}},
console = [{'script':"#######.py"}],
zipfile = None
)
I've already fixed an issue with zmq (which isn't ever used by my script, or my GUI, for that matter, as far as I know). What am I doing wrong?
Right, I've managed to get my app to build, and although the question is now moderately old, it's my hope this is eventually of use to someone.
Firstly, py2exe is probably the wrong tool. It's old and AFAICT unmaintained. Consider PyInstaller instead. Using PyInstaller is literally as simple as installing it, installing PyWin32, and then going python %path_to_pyinstaller%/pyinstaller.py --onefile --windowed source.py. PyInstaller deals with all the mess of side by side assemblies and so on without you having to do anything.
In short, use PyInstaller.
However, to answer your question, this worked for me:
The question you've linked to - in particular this answer is the right start. Find the right DLLs and copy them to C:\Python27\DLLs
Ditch your existing setup.py file. If you're not using zmq, there's no reason to import it. Also, for a windowed application you want windows= not console=. My file goes (for packaging show.py):
#!/usr/bin/python
from distutils.core import setup
import py2exe
setup(options={'py2exe':{'bundle_files':1}},
windows=['show.py'])
(This is pinched off http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/)