1

Im getting nowhere with the following error on my Raspberry Pi:

My own Python script calls a function from another module named BlackBean.py which in turn imports other modules called "netaddr" and "configparser". The problem is that I just cant seem to get past the import error which tells me " No Module named netaddr, or if I comment out that import then it also errors with No Module named configparser. So I know its a path issue but I just cant seem to get it fixed!

The Blackbean.Py script starts like this:

import broadlink
import ConfigParser
import sys, getopt
import time, binascii
import netaddr
import BlackBeanSettings
import re
from os import path
from Crypto.Cipher import AES

SettingsFile = ConfigParser.ConfigParser()
SettingsFile.optionxform = str
SettingsFile.read(BlackBeanSettings.BlackBeanControlSettings)

def execute_command(etc.........

The BlackBean.py file is in my project SkyHD folder at /home/pi/SkyHD. The "netaddr" and "configparser" files & folders were installed by pip in /home/pi/.local/lib/python2.7(and python3.5)/site-package folders.

sys.path has the above folders in its list and Ive also edited .bashrc and added PYTHONPATH=${PYTHONPATH}:/home/pi/.local/lib/python2.7/site-package:/home/pi/.local/lib/python3.5/site-package:/home/pi/SkyHD:../ but none of this works. I guess it must be something basic but I just cant work it out! help!

Also, some more info, when I first install all the files and run my program everything works fine and it finds the files ok with no problems, its only when I reboot it fails to find the files.

  • 1
    Do I understand that you have *both* the Python 2.7 *and* the Python 3.5 site-package folders in the *same* PYTHONPATH variable? If so, that's really not a good idea. – BoarGules May 11 '18 at 15:48
  • Ok, thanks for the info, but will that cause my problem? I cant see how. – Vince Williams May 11 '18 at 20:53
  • Can't you? Python 2.7 is first in the path, so if Python 3 is looking for a particular module, it will look first in the Python 2.7 directory and so it will get *the Python 2 version of that module* which may not be compatible with Python 3. That isn't just asking for trouble, it's *begging* for it. – BoarGules May 11 '18 at 21:04
  • But the error says it cant find ANY version of the module, so what you propose is good practice I agree, but wont solve the issue. – Vince Williams May 11 '18 at 21:40
  • You can't say that until you've tried it. But suit yourself. – BoarGules May 11 '18 at 21:52
  • Ill try it tomorrow, thanks – Vince Williams May 11 '18 at 22:00

2 Answers2

1

Its fixed. Python looks for imported modules in 3 places, the first being the folder you launched the python script from; so for me the obvious answer is to import the modules I need directly into my own Project folder (/home/pi/myproject). This worked just fine, it works every time even after reboot, which was my main problem before. No need to create or alter PYTHONPATH, no need to mess around with entries in .bashrc or try to change the python path entries. Here are the steps: Upgrade PIP to version 9.0.3 (not ver 10) with

pip install --upgrade pip==9.0.3

then install the required modules with the following

pip install --target=/home/pi/your_project_folder module_name

so for me it was... pip install --target=/home/pi/SkyHD netaddr

Im sure this is not best practice, but my Raspberry Pi only has this one project to run and having modules imported into the Projects folder just isnt an issue. Hope this helps some others with the same problem.

0

You've provided insufficient information. Specifically, details about the python command being used to run your script such as its version (python -V) and its module search path if you do

env -u PYTHONPATH python -c 'import sys; print(sys.path);'

Similarly you can easily simplify the problem. What happens if you do python -m netaddr?

Obviously in the above commands substitute the actual python command being used to run your script.

And, as @BoarGules mentioned in his comments to your question, you should never, ever add directories to PYTHONPATH for different python versions unless you know that the modules in those directories has been written to work with python2 and python3.

Kurtis Rader
  • 6,734
  • 13
  • 20