3

The book "Learning Python" by Mark Lutz states that in Python one can use various types of external modules, which include .py files, .zip archives, C/C++ compiled libraries and others. My question is, how does one usually handle installation of each type of module? For example, I know that to use a .py module, I simply need to locate it with import. What about something like .dll or .a? Or for example, I found an interesting library on GitHub, which has no installation manual. How do I know which files to import? Also, are there any ways of installing modules besides pip?

Oleksandr Novik
  • 489
  • 9
  • 24
  • 1
    In *most* cases you just ``pip install module`` the module and then ``import module `` – that also works for properly packaged zipped and compiled modules. Is there a *specific* module you are wondering about? The *possible* packaging ways are massive, it's not really feasible to describe them all. – MisterMiyagi Nov 29 '21 at 12:27
  • @MisterMiyagi yeah, I was wondering if I can use my 'homegrown' library I built in C. It has several functions which just generate random numbers. I would like to use it in my Python code. Is that possible? And how do I do that? – Oleksandr Novik Nov 29 '21 at 12:29
  • You might want to look at the [C-API description](https://docs.python.org/3.6/c-api/index.html) and the [Packaging Guide](https://packaging.python.org). I don't think laying it *all* out is focused enough for an [so] Q&A. – MisterMiyagi Nov 29 '21 at 12:33

2 Answers2

1

Also, are there any ways of installing modules besides pip?

Yes, according to Installing Python Modules (Legacy version) modules packaged using distutils should be downloaded, unpacked and command

python setup.py install

or similar should be run. Beware that

The entire distutils package has been deprecated and will be removed in Python 3.12.

Daweo
  • 31,313
  • 3
  • 12
  • 25
1

The answer depends on what you want to do. You can use Ninja for example to use C++ modules and cython for C and there are various packages for almost any type of compiled code. You can install packages via pip using the pypi package repository or by using cloned repositories that have a setup.py file inside. Any other python based repo can be imported either by a custom build script (that they will provide) or by directly importing the relevant Python files. This will require you the dive into the code and check what are the relevant files.

Tamir
  • 1,224
  • 1
  • 5
  • 18
  • So basically, most of the time, various repos on GitHub will provide me with the `setup.py` file which I will run via `python setup.py install` ? And about C++ libraries. Can't I just normally `import` `.dll` files I previously had built with CMake, for example? And do they need special way of building/linking or something? – Oleksandr Novik Nov 29 '21 at 12:27
  • 1
    You can't use .dll directly, you need some sort of a "bridge" to use them in python. This is where packages like Cython come in. You can sometimes use Pip directly on repos, check this:https://stackoverflow.com/questions/20101834/pip-install-from-git-repo-branch – Tamir Nov 29 '21 at 12:28