我如何使用第三方“库”?在Python中?
免责声明:这是一个非常基本的问题,但请记住,我来自 Microsoft 背景,并且我主要使用 Visual Studio 在 .NET 中进行编程(这也许可以帮助您进行类比)
我开始编写一些 Python 程序是为了好玩,所以我下载了 eclipse,并安装了 PyDev。
我到达了需要解析日期的地步,我找到了 time 的替代方案.strptime 看起来很有趣。这个替代方案是 python-dateutil。
我继续下载了它,但是当我尝试使用它时遇到了问题。
显然下载包含源文件,我完全不知道如何在我的应用程序中使用它日食中的“项目”。
所以我的非常基本的问题是:
- 我是否需要将源文件直接包含在我的文件中?也许在子目录中?我将如何在我的代码中使用它(如何导入它们到我的 .py 文件中)?
- 我需要“构建”(制作?)它们并引用它们吗?如何?如何在 Windows 中“编译”类似的东西?
- 我完全错过了一些重要的事情吗?
感谢您的耐心等待。
Disclaimer: This is a very basic question, but keep in mind I come from a Microsoft background, and I've programmed mostly in .NET with Visual Studio (this may help you help me with analogies perhaps)
I started programming a little python for the fun of it, so I downloaded eclipse, and installed PyDev.
I reached a point where I needed to parse a date, and I found an alternive to time.strptime that seemed interesting. That alternative was python-dateutil.
I went ahead and downloaded it, but I had problems when I tried to use it.
Apparently the download includes source files, and I had absolutely no idea how to use it in my "project" in eclipse.
So my very basic questions:
- Do I need to include the source files directly with my files? Perhaps in a subdirectory? How would I use it in my code (how do I import them in my .py file)?
- Do I need to "build" (make?) them and reference them? How? How do you "compile" something like that in windows?
- Am I totally missing some important point?
Thank you for your patience.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你应该在你的命令类型中使用 pip :
这个命令将为你做所有的事情将下载并安装该库
自动地。如果您没有
pip
请参阅上面的文档以了解如何安装它如果您像上面一样使用了 pip,则不需要,您现在只需在脚本中导入 dateutil 即可使用它:
一切都是用 pip 完成的(不是很神奇吗:))
是的,Python 很酷、简单且有趣,忘记 make、make install 吧……Python 中的所有内容都很容易安装。
为了 G.. 的热爱,请使用 pip (这是来自 BDFL 的命令)。
希望这可以帮助:)
you should use pip in your line of command type :
this command will do all the thing for you it will download and install the library
automatically. if you don't have
pip
refer to the documentation above to see how you should install itif you have used pip like above no need , you should now just
import dateutil
in your scripts to use it:everything is done with pip (magic isn't it :) )
Yes python is cool, easy and fun , forget about make ,make install ... Everything in python is easy installable.
And for the love of G.. please use pip (it's an order from the BDFL).
hope this can help:)
您下载的 tarball 中有一个 Makefile,因此只需使用它:
然后,在您想要使用新模块中的内容的文件中,像任何其他 Python 模块一样导入它:
The tarball you downloaded had a Makefile in it, so just use that:
Then, in the file where you want to use something from the new module, import it like any other Python module:
您只需导入 py 文件即可。例如,如果您的模块名为
x
,则需要执行import x
不需要。只需执行
import x
即可创建一个x.pyc
文件。这是模块x
的字节编译版本。然后您需要执行(在 Windows 上可能需要适当的权限。阅读更多内容此处)
python setup.py install
这将自动安装(并因此复制)所有模块文件到 Python 解释器可以找到它们的路径。您可以通过使用以下命令找到它:
import sys
,然后print sys.path
。这些将是解释器搜索模块的位置。安装后,启动解释器,然后尝试
import dateutil
。如果一切顺利,该模块应该被导入。当您需要分发应用程序时,需要打包所有必需的模块。请注意,您只需要包含
py
文件,而不是pyc
。为了更好地理解打包源文件,您需要阅读
disutitls
模块。 这里是链接。You just need to import the
py
file. For example, if your module is calledx
, you need to do aimport x
No. Just do an
import x
and ax.pyc
file will be created. This is the byte-compiled version of the modulex
.Then you need to do a (might require appropriate permissions on Windows. Read more here)
python setup.py install
This will automatically install (and thus copy) all the module files to a path where the Python interpreter can find them. You can find that by using:
import sys
and thenprint sys.path
. These will be the locations where the interpreter will search for the modules.After installation, start your interpreter and then try
import dateutil
. If all is well, the module should be imported.When you need to distribute your application, all the necessary modules will be need to be packaged along. Note that you just need to include the
py
files and notpyc
.For a better understanding of packaging the source files, you need to read about the
disutitls
module. Here is the link.您下载的 tarball 中有一个 Makefile,因此只需使用它:
make install
然后,在您想要使用新模块中的内容的文件中,像任何其他 Python 模块一样导入它:
import dateutil
The tarball you downloaded had a Makefile in it, so just use that:
make install
Then, in the file where you want to use something from the new module, import it like any other Python module:
import dateutil
解压存档并执行:
这里是详细信息。
Extract the archive and execute:
Here is details.