Using Libraries with python3

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Applications

Python3

References

https://github.com/brisbaneroboticsclub/IntroToPython3Libraries

https://realpython.com/absolute-vs-relative-python-imports/

Clone the Github code

Clone the repositories from Github to your robot.

cd

sudo git clone https://github.com/brisbaneroboticsclub/IntroToPython3Libraries.git

Possible Errors: fatal: destination path ‘IntroToPython3Libraries’ already exists and is not an empty directory.

Solution: If the directory already exists and you want to remove it, enter the following to delete ALL files in the IntroToPython3Libraries and the parent folder.

sudo rm -rf IntroToPython3Libraries

Change to the directory and change the file permissions so you can read/write/execute.

cd IntroToPython3Libraries

sudo chmod 777 mainimport.py

sudo chmod 777 <file/folder>

Libraries

Open the program called ‘mainsubimport.py from the ‘subfolder’ directory’. This will be the library we call.

python3 mainsubimport.py

This library can run on it’s own and has it’s own demo.

Now Call it from another program.

Open the program called ‘mainimport.py’ and import ‘mainsubimport.py’

python3 mainimport.py

Please note the lines which are “commented” (Lines 6-7 and 49-50).

See the difference between importing and from-importing?

Also notice, the demo() from the library didn’t execute? This is because ‘mainsubimport.py’ uses the “if name == __main__” function.

Any ‘imports’ required in the imported libraries will be done automatically within the imported programs. There is no need to re-import them in the main program.

Did it work?

If you had an issue or suggestion, please report it on the our github page.

https://github.com/brisbaneroboticsclub/IntroToPython3Libraries/issues

Can you help make this better?

Do you have an idea to make this project better? Please leave a comment below…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.