eclipse 如何在 python 中使用第三方“库”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4109206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use third party 'libraries' in python?
提问by juan
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)
免责声明:这是一个非常基本的问题,但请记住,我来自 Microsoft 背景,并且我主要使用 Visual Studio 在 .NET 中进行编程(这可能会帮助您进行类比)
I started programming a little python for the fun of it, so I downloaded eclipse, and installed PyDev.
为了好玩,我开始编写一个小 python,所以我下载了eclipse,并安装了PyDev。
I reached a point where I needed to parse a date, and I found an alternive to time.strptimethat seemed interesting. That alternative was python-dateutil.
我到了需要解析日期的地步,我找到了一个看起来很有趣的 time.strptime替代方法。该替代方案是python-dateutil。
I went ahead and downloaded it, but I had problems when I tried to use it.
Apparently the downloadincludes 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 importthem 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?
- 我需要在我的文件中直接包含源文件吗?也许在子目录中?我将如何在我的代码中使用它(如何将它们导入到我的 .py 文件中)?
- 我是否需要“构建”(制作?)它们并引用它们?如何?你如何在 Windows 中“编译”类似的东西?
- 我完全错过了一些重要的点吗?
Thank you for your patience.
感谢您的耐心等待。
回答by mouad
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 pipin your line of command type :
你应该在你的命令类型中使用pip:
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
这个命令会为你做所有的事情,它会自动下载和安装库。如果你没有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)?
我需要在我的文件中直接包含源文件吗?也许在子目录中?我将如何在我的代码中使用它(如何将它们导入到我的 .py 文件中)?
if you have used pip like above no need , you should now just import dateutil
in your scripts to use it:
如果你已经使用了 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?
我是否需要“构建”(制作?)它们并引用它们?如何?你如何在 Windows 中“编译”类似的东西?
everything is done with pip (magic isn't it :) )
一切都是用 pip 完成的(不是很神奇吗:))
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.
是的,python 很酷,简单又有趣,忘记 make ,make install ... python 中的一切都很容易安装。
And for the love of G.. please use pip (it's an order from the BDFL).
对于 G.. 的爱,请使用 pip(这是来自BDFL的订单)。
hope this can help:)
希望这可以帮助:)
回答by nmichaels
The tarball you downloaded had a Makefile in it, so just use that:
您下载的 tarball 中有一个 Makefile,所以只需使用它:
make install
Then, in the file where you want to use something from the new module, import it like any other Python module:
然后,在要使用新模块中某些内容的文件中,像导入任何其他 Python 模块一样导入它:
import dateutil
回答by user225312
- 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 文件中)?
You just need to import the py
file. For example, if your module is called x
, you need to do a import x
您只需要导入py
文件。例如,如果你的模块被调用x
,你需要做一个import x
- Do I need to "build" (make?) them and reference them? How? How do you "compile" something like that in windows?
- 我是否需要“构建”(制作?)它们并引用它们?如何?你如何在 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
.
不。只要做一个import x
,x.pyc
就会创建一个文件。这是模块的字节编译版本x
。
- Am I totally missing some important point? Download the
python-dateutil
and extract it to a directory.
- 我完全错过了一些重要的点吗?下载
python-dateutil
并解压到一个目录。
Then you need to do a (might require appropriate permissions on Windows. Read more here)
然后你需要做一个(在 Windows 上可能需要适当的权限。在这里阅读更多)
python setup.py install
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.
这将自动安装(并因此复制)所有模块文件到 Python 解释器可以找到它们的路径。您可以通过使用:import sys
然后找到print sys.path
。这些将是解释器搜索模块的位置。
After installation, start your interpreter and then try import dateutil
. If all is well, the module should be imported.
安装后,启动您的解释器,然后尝试import dateutil
. 如果一切顺利,应该导入模块。
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
.
当您需要分发应用程序时,所有必要的模块都需要打包在一起。请注意,您只需要包含py
文件而不是pyc
.
For a better understanding of packaging the source files, you need to read about the disutitls
module. Hereis the link.
为了更好地理解打包源文件,您需要阅读有关该disutitls
模块的信息。这是链接。