简体   繁体   中英

Where should I import Python libraries in my source file?

I just downloaded Pygame but I'm not sure how to import it into my program.

Where do I put the folder that contains Pygame? Within the python2.7 folder?

It all depends on where you want to put it. Python can be told to search for and load modules from any location you want, so long as you have read permissions. See the python documentation on Modules , particularly the section on the module search path.

The Pygame modules could, for ease of use, be placed in the same directory you're executing your python script from, or alternately your script could append the libraries path as directed in the above link, and then try to import the module(s) as appropriate.

How to install PyGame

Read the documentation :

Windows Binary Installer

This is probably the most popular method of installation. If you are running on windows, it is highly recommended you use this form of installing. The installers come with with nearly everything you need, and have an easy point and click installers.

...

So basically download version for Windows and it should be very easy to install (includes a wizard).

How to import module

When it comes to importing, this is as simple as importing any other module:

import some_module

Specifically, PyGame can be imported like that (see tutorials ):

import pygame
from pygame.locals import *

How to check if the module was found

This should work, as the installer should make sure the module is on the path Python is searching for the modules. To see where exactly Python searches for modules, do this in the console:

import sys
print sys.path

If PyGame was installed on one of these paths, then everything should be okay.

Start python, import sys, and print out sys.path. That's the search path that Python uses to find imports. You would normally put it in dst_packages.

import sys
print sys.path

From there you can see where python is looking.

I'm sure you could always go with a 'pip' install as well.

Do you have an error message you are receiving?

you can place the lib any where you like.

you may need to see this tutorial: http://docs.python.org/tutorial/modules.html

I took a look at the Pygame download instructions and it seems like the downloadable .exe should probably take care of the nitty-gritty for you. Do you see a setup.py file in the directory after you run the .exe ? If so, you should be able to run python setup.py install from that directory to have it 'properly' installed (note: I've never used the installer, so it may very well bypass that step completely).

As far as where you would need put any given module, it's basically like @Adrian says - it's all where you want to put it. @tijko's post will show you exactly where Python looks when you use the import statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM