简体   繁体   English

python“导入媒体”无效,但存在“ media.py”

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

I read a book to learn python programming, it showed the code : 我读了一本书来学习python编程,它显示了代码:

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 . 因此,我从链接http://pragprog.com/titles/gwpy/source_code下载了gwpy-code.zip并安装了PyGraphics-2.0.win32.exe In the path C:\\Python27\\Lib\\site-packages\\pygraphics there really was media.py ! 在路径C:\\Python27\\Lib\\site-packages\\pygraphics ,确实存在media.py But why import media didn't work ? 但是,为什么import media无效? (ps: I also tried this C:\\Python27\\Scripts\\easy_install nose in the DOS box, still not work...) Best Regards :) (ps:我还在DOS框中尝试过C:\\Python27\\Scripts\\easy_install nose ,仍然无法正常工作...)最好的问候:)

Try: 尝试:

from pygraphics import media

If you're not familiar with importing modules in Python yet, a brief primer might be useful. 如果你不熟悉Python中导入模块呢,一个简短的引物可能是有用的。

>>> 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. C:\\Python27\\Lib\\site-packages\\应该是其中之一。

To import a file located on your system path, you can use import filename (for filename.py). 要导入位于系统路径上的文件,可以使用import filename (对于filename.py)。 If the file lies in a subdirectory, eg dir1/dir2/filename.py , it can be imported using import dir1.dir2.filename . 如果文件位于子目录(例如dir1/dir2/filename.py ,则可以使用import dir1.dir2.filename

Note: A directory acts as a 'package' if it contains a file called __init__.py . 注意:如果目录包含名为__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. 原因是Python在site-packages目录中查找软件包。 The file media.py is within the folder (and therefore the package) pygraphics , so you can't get to it directly. 该文件media.py是文件夹(因此包装)内pygraphics ,所以你不能直接访问它。

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

你可能不得不说

from pygraphics import media

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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