How can I use third party 'libraries' in python?

回眸只為那壹抹淺笑 提交于 2019-11-30 17:39:01

问题


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.


回答1:


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

hope this can help:)




回答2:


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



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

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.




回答4:


Extract the archive and execute:

python setup.py install

Here is details.



来源:https://stackoverflow.com/questions/4109206/how-can-i-use-third-party-libraries-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!