-1

Usually, python libraries such as numpy or matplotlib are used with "import numpy" in a .py script, then we call the functions we imported.

However some libraries such as "pip" or "anaconda" are not imported in a script, but rather they are command-based: called from a terminal using arguments and options such as "pip install [options] [whatever]".

Today I installed a library and it took me a while to realise it was command-based, and wouldn't work if I just ran the script "main.py" it contained.

My question is: what is the fundamental difference between those 2 kind of libraries? How do I learn to make command-based libraries?

Basically I just want to understand them, but don't even know where to begin my research.

KiwiKiwi
  • 19
  • 2
  • Does this answer your question? [Run function from the command line](https://stackoverflow.com/questions/3987041/run-function-from-the-command-line) – MShakeG Apr 20 '20 at 18:17
  • I don't want to run functions from the command line like "python ...". I want to run a library like "library ...". The same way you don't type "python pip install" but "pip install" directly. – KiwiKiwi Apr 20 '20 at 18:41
  • the issue is that pip is a package manager application, hence you don't need to have "python pip", as python isn't a dependency for pip, since pip is an independent executable program and has its path added to environmental variables making it accessible from the command line. Wheras a python script would require "python ..." as the python interpreter is a necessary dependency. I'm not sure how packages like pip work exactly, but from my knowledge I would code up a batch file to run the package which would then enter a command mode similar to the python interpreter. – MShakeG Apr 20 '20 at 19:51
  • additionally you can give the batch an alias so 'libName.bat' can be called with 'libName' by giving it an alias using: doskey libName=libName.bat NB:don't forget to add the batch file directory to path in environment variables. – MShakeG Apr 20 '20 at 20:04

1 Answers1

0

I want to run a library like "library ...".

There is a major misunderstanding here. pip is both a command-line script and a library.

The script resides in bin/pip, for example /usr/bin/pip (on w32 it's Scripts\pip).

The library resides somewhere in sys.path, perhaps in site-packages/pip, for example /usr/lib/python3.8/site-packages/pip/.

And of course the command-line script is written in Python and it imports the library pip. This main.py is converted to script pip by setuptools.

If you want to do the same — create a command-line script that imports your library — you have to go the same route. Create a library and a script or an entry points.

phd
  • 82,685
  • 13
  • 120
  • 165