quinnsimmons
Posts: 14
Joined: Wed Nov 19, 2014 11:26 pm

Where to install new Python libraries/modules?

Wed Nov 19, 2014 11:32 pm

I'm really new to programming with the Pi, and I'm trying to set up the ADS1015 ADC using the following guide: http://openlabtools.eng.cam.ac.uk/Resou ... i_ADS1115/.
In section 7, I'm told to change directories to "Documents" and to install the ADS1X15 libraries there. But, when I tried this before, the Pi could not find these libraries/modules, so where should I install them so that when I use the "import" command in Python, they can be found? I'm using Raspbian as my OS. Thanks!

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Where to install new Python libraries/modules?

Thu Nov 20, 2014 12:02 am

What I tend to do is copy the module(s) I need to the directory where my script is.
There are other ways e.g. extending the environment var PYTHONPATH. It works the same as the standard 'PATH' environment variable.
You can change this on the command line, or .bashrc or any other appropriate place.
For example:

Code: Select all

PYTHONPATH=$PYTHONPATH:/home/pi/mymodules 
adds /home/pi/mymodules to the search path.
More info on where and how python looks for modules: https://docs.python.org/2/tutorial/modu ... earch-path

Gr.
Dirk.

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Where to install new Python libraries/modules?

Thu Nov 20, 2014 12:47 am

I like to put my common modules in /usr/local/lib/python2.7/dist-packages/ (I'm the admin, I can do that) which is typically where a 'sudo apt-get install' puts 'em. No messing with PYTHONPATH. Adjust for your Python version, of course.

quinnsimmons
Posts: 14
Joined: Wed Nov 19, 2014 11:26 pm

Re: Where to install new Python libraries/modules?

Thu Nov 20, 2014 5:08 am

Ok, I think I'll put them in with the Python libraries just to keep things simple; both are great tips, though. So my question now is, since the library/module files (http://github.com/adafruit/Adafruit-Ras ... n-Code.git) are in their own folders, do I need to copy and paste the contents of each individual folder into the /usr/local/lib/python2.7/dist-packages/ directory? Or is it sufficient to simply paste each folder into that directory? Thanks again!

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Where to install new Python libraries/modules?

Thu Nov 20, 2014 5:26 am

Folders in the path will be searched recursively.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Where to install new Python libraries/modules?

Fri Nov 21, 2014 10:17 am

I put this in

Code: Select all

import sys
sys.path.insert(1, '/home/pi/xyz')
import xyz
The advantage is that you can also install the official package 'automatically' (pip/setup.py install etc) without running into trouble hacking dist-packages or PYTHONPATH. By inserting at position 1, rather than appending, the version you have in /home/pi/xyz (latest developments brought in using 'git clone https://github.com/xyz/xyx.git' say) will be picked in preference to the 'installed' version from dist-packages. You can take the line out to revert to the installed version.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”