How to create a Library for import


I’m writing a dronekit flight script, and want to import a custom library. Where are dronekit libraries like “import”, “connect”, “VehicleMode”, and other general libraries like “time”, “argparse”, etc. saved in the Ubuntu environment?

Is it:

/usr/local/lib/python2.7/dist-packages ?

Or does it depend on how everything was downloaded. Do I need to make any changes in my bashrc so the drone knows where to access libraries from?

Similarly, if I wanted to import a new library to my actual Raspberry Pi drone, where would be the best spot to save it in the Pi? Would it have to be downloaded from github?

Thanks for any help, this is all relatively new for me.


It is correct that packages installed using pip (or sudo apt-get) will live in /usr/local/lib/python2.7/dist-packages or /usr/local/lib/python3.X/dist-packages for python 3.X.

When using the import statement in Python, you have to specify a .py file that you wish to import. When python interprets this import statement, for example a statement such as ‘import mymodule’, it looks for a file called . The question then arises, where does python look for this file? The answer is that python looks in all locations that are specified in the python path. You can see all of these locations in the path by running the following from a terminal: python -c “import sys; print(sys.path)”

So when importing a module, python first looks in the current directory from where the python script was run from, and then in the default locations (which are specified in sys.path, and /usr/lib/local/pythonX.Y/dist-packages).

The simplest way to import your own python modules in a different python script, is to keep the module in the same directory as the script that imports it, and use an import statement that just references that module by it’s filename.