1

I'm writing a library for Python, and it uses Pygame. To make it more mobile and user-friendly, I want to be able to import Pygame like you can import jQuery in JavaScript/ HTML. I have the Pygame zip file, but I have not been able to find any sort of help. Here's what I'm talking about, but written in HTML:

<script src="example.js"></script>

In python, it should look something like

import Pygame

Please try to explain this in beginner language - as simple as possible, and if possible - with examples.

Thanks :D

AlgoRythm
  • 115
  • 10

1 Answers1

2

You have first to uncompress the zip-archive file. Then, make sure you have a folder called Pygame with a file __init__.py. Finally, you can import it with import Pygame after you moved it in the site-packages folder, or you can import it using the following code :

import sys
sys.path.append('/Path/to/the/directory/containing/the/Pygame/folder')
import Pygame

The explanation is that Python looks for the modules you want to import only in the folders in the sys.path list (you can display it and see that it contains the path of site-packages). Thus, if the module is not in a standard location, you have to add its path to this list.

EDIT: Sorry, my bad, I thought it was possible to run Pygame without any install. But, after I looked in the downloaded file, it requires installation. I think this question is a duplicate of Using Pygame without installing

Community
  • 1
  • 1
Labo
  • 2,482
  • 2
  • 18
  • 38
  • I tried 'import sys sys.path.append('pygame-1.9.1release\pygame-1.9.1release\lib\__init__.py') import pygame' and it didn't work, it threw the error "No module named pygame" – AlgoRythm Oct 28 '15 at 18:42
  • there are proper line breaks, btw. I didnt know how to do that in a comment – AlgoRythm Oct 28 '15 at 18:43
  • You cannot break lines in a comment. You have to append the path of the folder that _contains_ the Pygame folder. And you should remove the 'dll' tag. If you want to have the exact text you typed displayed, surround it with backticks : ` – Labo Oct 28 '15 at 18:44
  • there is no "Pygame" folder, I have a folder named Pygame-1.9-release and inside that is the lib folder, which has an init inside of it. I appended the containing folder (lib) and imported pygame but still got the error – AlgoRythm Oct 28 '15 at 18:51
  • I don't really understand the answer my question is a 'duplicate' of – AlgoRythm Oct 28 '15 at 23:17
  • Because in fine you want to use pygame without installing it. – Labo Oct 29 '15 at 15:26