pyboard.py and PYTHONPATH

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
LRonHorse
Posts: 9
Joined: Thu Aug 13, 2015 10:20 am
Location: St Ives Cambridgeshire
Contact:

pyboard.py and PYTHONPATH

Post by LRonHorse » Thu Aug 27, 2015 5:44 pm

Hi folks
I've got an annoying problem that's part pyboard related and part python related so I hope this is appropriate!
I'm running the pyboard connected to a Raspberry Pi 2 with Raspbian as the OS.
Pyboard.py works fine with a small test file if I have both in the directory that I issue the command 'python3 pyboard.py test.py' from.
However, if I put both files - pyboard.py and test.py - in a PYTHONPATH directory then I get the message:
"FileNotFoundError: [Errno 2] No such file or directory: <filename>
when I run 'python3 pyboard.py test.py' from my home directory - which is not in PYTHONPATH.

To try and sort out this problem I've added a directory to my home directory called 'localpython' and added this to PYTHONPATH by adding 'export PYTHONPATH =$PYTHONPATH : $HOME/localpython' to my .profile.
When i check PYTHONPATH (using import sys and print(sys.path) from within python3) it shows the correct path has been added along with the standard directories.
Just in case, I've also set my RPi path to include /home/LRon/localpython but still no joy.
I'm fairly sure it's something either trivial or stupid so if anyone has any ideas as to how I might fix this I'd be most grateful as I'd like to stash pyboard.py and rshell in a single directory on PYTHONPATH instead of copying them to various directories.

Cheers

LRon

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: pyboard.py and PYTHONPATH

Post by dhylands » Thu Aug 27, 2015 8:05 pm

PYTHONPATH only affects import statements, and doesn't affect the path of the python script being passed to the interpreter.

So the correct invocation should be:

Code: Select all

python3 path-to-pyboard.py-script/pyboard.py path-to-test.py-script/test.py
The line at the top of the pyboard.py script has

Code: Select all

#!/usr/bin/env python
so if your system is setup to have python3 be the default (you can test this by trying to run /usr/bin/env python at the command line and seeing which version of python is reported. You can either configure your system to make python 3 be the default or replace the /usr/bin/env python with /usr/bin/python3 (or whatever is appropriate for your environment) and then you can make the pyboard.py script executable (chmod +x pyboard.py) and put it someplace in your PATH (not PYTHONPATH) and then you'd be able to do:

Code: Select all

pyboard.py path-to-test-script/test.py
If path-to-test-script is a relative path, then it will be relative to the current directory when you invoke pyboard.py and not the location of pyboard.py.

Hopefully that makes sense. If not feel free to ask for further clarification.

User avatar
LRonHorse
Posts: 9
Joined: Thu Aug 13, 2015 10:20 am
Location: St Ives Cambridgeshire
Contact:

Re: pyboard.py and PYTHONPATH

Post by LRonHorse » Fri Aug 28, 2015 6:19 pm

Thanks for that Dave, you gave me some useful pointers :)

I thought what you said was the case with PYTHONPATH but wasn't entirely sure - thanks for confirming that.

My python system default is standard for Raspbian - python 2.7 confirmed with /usr/bin/env python - but running python3 with appropriate relative or absolute path/filenames gave correct results.
I added ~/localpython to my path and for good measure added 'alias python=python3 to .bashrc and checked that they worked, which they did.
Next, setting #!/usr/bin/env python3 or #!/usr/bin/python3 in the file didn't work and gave an error message saying that 'serial' could not be imported. Head scratch time!
What I eventually figured out was that even though pointing to python3 in every possible place the environment default stays with 2.7 and I didn't want to change this as it might affect other applications using 2.7 as default. Also, even though I had apparently installed pyserial for both python versions they live in very different places.
For 2.7, serial can be found at /usr/lib/python2.7/dist-packages/serial and for 3.4 at /usr/local/lib/python3.4/site-packages/serial !!!
Adding the 2.7 serial path to PYTHONPATH (for both 2.7 and 3.4 in .profile) seems to have fixed the problem, although I'm not sure if this is an entirely legal fix.
I'll have another look at this in the future to see if I can find a better solution but for now, as it appears to work, I'll get on and install your rshell code and play with that.
Once again, thanks Dave

LRon

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: pyboard.py and PYTHONPATH

Post by dhylands » Fri Aug 28, 2015 9:30 pm

I think that both python2 and python3 use the same PYTHONPATH variable, so you probably don't want to be mixing and matching.

I normally don't set PYTHONPATH at all (i.e. my PYTHONPATH is empty) and let the installation figure out where its stuff is installed (running on desktop Ubuntu).

Having both 2.7 and 3 in PYTHONPATH feels like a recipe for disaster in my books. You definitely want each version of python to use the library that belongs to that version of python.

To really isolate things you can use a virtualenv. I'm not familiar with running python on the Raspberry Pi, so I can't offer much help there.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: pyboard.py and PYTHONPATH

Post by pythoncoder » Sat Aug 29, 2015 5:28 am

+1 for avoiding PYTHONPATH. Each version of Python has its own set of standard libraries and "knows" where to find them. Avoid doing anything that interferes with that. As @dhylands pointed out, well written PC Python programs start with a shebang which points them at the correct version of Python; any import statements will then pull in the appropriate standard libraries. If a Python 3 program fails to find a standard library you'll need to install it rather than attempt to point Python at an older version.

I make extensive use of aliases to simplify things so I have one alias to take me to my project directory and another so I can start rshell from any directory:
alias rshell='/mnt/qnap2/python/rshell/rshell.py'
For this to work you need to give rshell.py (or pyboard.py) execute privilege (sudo chmod 777 pyboard.py).

In other words my advice is trust the program to pull in the right version of Python, and trust Python to import the correct standard libraries!
Peter Hinch
Index to my micropython libraries.

User avatar
LRonHorse
Posts: 9
Joined: Thu Aug 13, 2015 10:20 am
Location: St Ives Cambridgeshire
Contact:

Re: pyboard.py and PYTHONPATH

Post by LRonHorse » Mon Sep 14, 2015 12:50 pm

Hi sorry it's taken so long to get back to you - I got a bit sidetracked and had to drop off the pyboard for a bit.
Thanks for the comments - especially about mixing Py2 & Py3 paths as this wasn't a good solution at all but, at the time seemed to be the only fix. However....
The mistake I made was a real newbie mistake and after looking at it this weekend just gone I realised that the #! instruction was referencing Python and not Python3 - once I corrected that mistake and added '#!/usr/bin/env python3' it all dropped into place!
My standard environment on the Raspberry Pi is set for Python 2.7 but adding the above and removing all the previous pythonpath rubbish it seems to be working now.

Cheers guys

LRon

Post Reply