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

发布于 2024-10-01 01:01:26 字数 899 浏览 4 评论 0原文

免责声明:这是一个非常基本的问题,但请记住,我来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北恋 2024-10-08 01:01:26

我继续下载了它,但是我
当我尝试使用它时遇到问题。
显然下载内容包括
源文件,我绝对没有
想法如何在我的“项目”中使用它
日食。

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

pip install python-dateutil

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

我需要包含源文件吗
直接使用我的文件?也许在一个
子目录?我将如何在我的
代码(如何将它们导入到我的 .py
文件)?

如果您像上面一样使用了 pip,则不需要,您现在只需在脚本中导入 dateutil 即可使用它:

import dateutil

我需要“构建”(制作吗?)它们并且
参考他们?如何?你怎么样
“编译”类似的东西
窗户?

一切都是用 pip 完成的(不是很神奇吗:))

我是否完全错过了一些重要的事情
点?

是的,Python 很酷、简单且有趣,忘记 make、make install 吧……Python 中的所有内容都很容易安装。

为了 G.. 的热爱,请使用 pip (这是来自 BDFL 的命令)。

alt text

希望这可以帮助:)

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.

you should use pip in your line of command type :

pip install python-dateutil

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 it

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

if you have used pip like above no need , you should now just import dateutil in your scripts to use it:

import dateutil

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

everything is done with pip (magic isn't it :) )

Am I totally missing some important
point?

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

alt text

hope this can help:)

痴情换悲伤 2024-10-08 01:01:26

您下载的 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
束缚m 2024-10-08 01:01:26
  • 我需要将源文件直接包含在我的文件中吗?也许在子目录中?我将如何在我的代码中使用它(如何将它们导入到我的 .py 文件中)?

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

  • 我需要“构建”(制作?)它们并引用它们吗?如何?如何在 Windows 中“编译”类似的东西?

不需要。只需执行 import x 即可创建一个 x.pyc 文件。这是模块 x 的字节编译版本。

  • 我是否完全错过了一些重要的点?
    下载 python-dateutil 并将其解压到目录中。

然后您需要执行(在 Windows 上可能需要适当的权限。阅读更多内容此处

python setup.py install

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

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

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

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

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

You just need to import the py file. For example, if your module is called x, you need to do a import x

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

No. Just do an import x and a x.pyc file will be created. This is the byte-compiled version of the module x.

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

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 then print 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 not pyc.

For a better understanding of packaging the source files, you need to read about the disutitls module. Here is the link.

素手挽清风 2024-10-08 01:01:26

您下载的 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

凉墨 2024-10-08 01:01:26

解压存档并执行:

python setup.py install

这里是详细信息。

Extract the archive and execute:

python setup.py install

Here is details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文