如何在python中使用第三方“库”?

2024-03-29 10:49:33 发布

您现在位置:Python中文网/ 问答频道 /正文

免责声明:这是一个非常基本的问题,但请记住,我来自Microsoft背景,并且我主要使用Visual Studio在.NET中编程(这可能有助于我进行类比)


为了好玩,我开始编写一个小python,所以我下载了eclipse,并安装了PyDev

我到了需要解析日期的时候,发现了一个time.strptime的替代项,看起来很有趣。另一种选择是python-dateutil

我继续下载,但在尝试使用时遇到了问题。
显然the download包含源文件,我完全不知道如何在eclipse中的“项目”中使用它。

所以我最基本的问题是:

  • 是否需要将源文件直接包含在我的文件中?可能在子目录中?如何在代码中使用它(如何在.py文件中导入它们)?
  • 我需要“建造”吗他们和参考他们?怎样?如何在windows中“编译”类似的东西?
  • 我是不是完全错过了一些重要的事情?

谢谢你的耐心。


Tags: 文件the声明nettime编程microsoft背景
3条回答
  • 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)?

您只需要导入py文件。例如,如果模块名为x,则需要执行import x

  • Do I need to "build" (make?) them and reference them? How? How do you "compile" something like that in windows?

不。只要做一个import x,就会创建一个x.pyc文件。这是模块x的字节编译版本。

  • Am I totally missing some important point? Download the python-dateutil and extract it to a directory.

然后您需要执行(可能需要在Windows上具有适当的权限)。阅读更多here

python setup.py install

这将自动安装(并因此复制)所有模块文件到Python解释器可以找到它们的路径。您可以通过使用:import sys然后使用print sys.path找到它。这些将是口译员搜索模块的位置。

安装后,启动解释器,然后尝试import dateutil。如果一切正常,应该导入模块。

当您需要分发应用程序时,需要将所有必需的模块打包在一起。注意,您只需要包含py文件,而不是pyc

为了更好地理解源文件的打包,您需要阅读disutitls模块。Here是链接。

你下载的tarball中有一个Makefile,所以使用它:

make install

然后,在要使用新模块中的某些内容的文件中,像导入任何其他Python模块一样导入它:

import 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.

您应该在命令类型的行中使用pip

pip install python-dateutil

这个命令将为您做所有事情,它将下载并安装库 自动。如果您没有pip,请参阅上面的文档,了解应该如何安装它

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)?

如果没有必要使用上述pip,那么现在应该在脚本中import dateutil使用它:

import dateutil

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?

是的,python很酷,简单有趣,忘记make,make install。。。python中的所有东西都很容易安装。

为了G.的爱。。请使用pip(这是来自BDFL的订单)。

alt text

希望这能有所帮助:)

相关问题 更多 >