MOCKSTACKS
EN
Questions And Answers

More Tutorials









Raspberry External Function Library


In previous lessons, we defined and used functions in the program But if we want to rewrite a new program, all the functions and variables we defined will disappear.

Python provides a solution to this problem by storing these programs that define functions or variables in specific files. When we want to use these definitions, we only need to use “import" at the beginning of the program to refer to this file, just like we do for a library file.

In short, a library file is a file that contains all the functions and variables you define, with the extension “.py". Library files can be imported by other programs to use the functions in the library files.

For Python, libraries are generally divided into two categories. One is the standard library of python, which contains many function libraries commonly used in project development. In previous lessons, the machine library and utime library we used are all the standard libraries of Python.

Another category is the third-party library developed by Python users. Thanks to the growth of the Python community, you can find many very helpful function libraries when using Python to complete projects.

For standard libraries, we don't need to do extra work when using them. We can simply refer to them directly in the program. For third-party libraries, however, we need to save library files onto the Pico before using them.

For example, in this lesson, if we want to use a third-party library to control the Grove - 16 x 2 LCD, we need to save it to Pico first.

1. Click to download the third-party library file lcd1602.py (7kB) we want to use, and save it to the computer.

2. Connect Pico to the computer, use Thonny to open the file, click on the “file" option at the top left, and select “save as".

When saving, Thonny asks you to name the file. Here we type “lcd1602.py" as its name. Note that we also need to type the extension of the file, otherwise it cannot be used.



Conclusion

In this page (written and validated by ) you learned about Raspberry External Function Library . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Display Hello world with LCD.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.