简体   繁体   English

我应该在哪里导入源文件中的Python库?

[英]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. 我刚刚下载了Pygame,但我不确定如何将它导入我的程序。

Where do I put the folder that contains Pygame? 我在哪里放置包含Pygame的文件夹? Within the python2.7 folder? 在python2.7文件夹中?

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. 只要您具有读取权限,就可以告诉Python从您想要的任何位置搜索和加载模块。 See the python documentation on Modules , particularly the section on the module search path. 请参阅有关模块的python文档,特别是有关模块搜索路径的部分。

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. 为了便于使用,Pygame模块可以放在您正在执行python脚本的同一目录中,或者您的脚本可以按照上述链接中的指示附加库路径,然后尝试导入模块(s) ) 作为适当的。

How to install PyGame 如何安装PyGame

Read the documentation : 阅读文档

Windows Binary Installer Windows二进制安装程序

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. 如果您在Windows上运行,强烈建议您使用这种安装形式。 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). 所以基本上下载Windows版本,它应该很容易安装(包括一个向导)。

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 ): 具体来说,可以像这样导入PyGame(参见教程 ):

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. 这应该工作,因为安装程序应确保模块位于Python正在搜索模块的路径上。 To see where exactly Python searches for modules, do this in the console: 要查看Python搜索模块的确切位置,请在控制台中执行以下操作:

import sys
print sys.path

If PyGame was installed on one of these paths, then everything should be okay. 如果PyGame安装在其中一条路径上,那么一切都应该没问题。

Start python, import sys, and print out sys.path. 启动python,导入sys,然后打印出sys.path。 That's the search path that Python uses to find imports. 这是Python用于查找导入的搜索路径。 You would normally put it in dst_packages. 你通常会把它放在dst_packages中。

import sys
print sys.path

From there you can see where python is looking. 从那里你可以看到python正在寻找的地方。

I'm sure you could always go with a 'pip' install as well. 我相信你也可以随时安装'pip'。

Do you have an error message you are receiving? 您收到错误消息吗?

you can place the lib any where you like. 您可以将lib放在任何您喜欢的位置。

you may need to see this tutorial: http://docs.python.org/tutorial/modules.html 您可能需要查看本教程: 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. 我看了一下Pygame 下载说明 ,似乎可下载的.exe应该可以为你处理细节问题。 Do you see a setup.py file in the directory after you run the .exe ? 运行.exe后,您在目录中看到setup.py文件吗? 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). 如果是这样,你应该能够从该目录运行python setup.py install以“正确”安装它(注意:我从未使用过安装程序,因此它可能完全绕过该步骤)。

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. 至于你需要放置任何给定模块的地方,它基本上就像@Adrian所说 - 它就是你想要放置它的地方。 @tijko's post will show you exactly where Python looks when you use the import statement. @tijko的帖子将显示Python在使用import语句时的确切位置。

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

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