Creating and using your own modules/libraries (Python3)

--Originally published at Elu's Blog

A library is a Python3 file that contains functions. We have already seen how to create a function (here is a link to the post were I explained them: The way to create a library is just to create a Python3 file and add functions. Then to use them in another code, the ‘import’ method is required. It’s very important that the two files (the library and the code you are working on) are in the same folder.

Here is an example:

  1. First, a random file with functions in it. (This is what I am using as a library): Captura de pantalla 2017-04-20 a la(s) 18.21.20.png
  2. Then a file where I use a function from my ‘library’:Captura de pantalla 2017-04-20 a la(s) 18.21.05.png

As you can see first I wrote ‘from’ then the name of the library file (in this case ‘SPE17’), then ‘import’ and to finish the function that I want to use.

There is another way, where you write ‘import’ and the library filename. This will import all the functions from that library but to use them you will have to write ‘[library filename].[function]’. In the example above I would’ve wrote ‘import SPE17’ in the first line, and when would’ve wanted to use the function I would’ve wrote ‘SPE17.phrase_palindrome()’.

Here is an alternative explanation:

Python 3 Programming Tutorial – Making Modules by sentdex