简体   繁体   中英

python “import media” didn't work but there was “media.py”

I read a book to learn python programming, it showed the code :

import media

So I downloaded gwpy-code.zip from the link http://pragprog.com/titles/gwpy/source_code and installed PyGraphics-2.0.win32.exe . In the path C:\\Python27\\Lib\\site-packages\\pygraphics there really was media.py ! But why import media didn't work ? (ps: I also tried this C:\\Python27\\Scripts\\easy_install nose in the DOS box, still not work...) Best Regards :)

Try:

from pygraphics import media

If you're not familiar with importing modules in Python yet, a brief primer might be useful.

>>> import sys
>>> print sys.path

If you try the code above, you will see a bunch of directories on your system path. C:\\Python27\\Lib\\site-packages\\ should be one of these.

To import a file located on your system path, you can use import filename (for filename.py). If the file lies in a subdirectory, eg dir1/dir2/filename.py , it can be imported using import dir1.dir2.filename .

Note: A directory acts as a 'package' if it contains a file called __init__.py . A file that can be imported is called a 'module'.

You need to do:

from pygraphics import media

The reason is that Python looks in the site-packages directory for packages. The file media.py is within the folder (and therefore the package) pygraphics , so you can't get to it directly.

导入媒体之前,必须导入ampy。

你可能不得不说

from pygraphics import media

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