PYTHONPATH and pip3 Custom Package Location on OpenWrt

I installed python3 and pip3 on my OpenWrt build, but don't have enough space for other python packages. So, I installed my custom packages via pip3 -t /mnt/sda1/.config/python3.

However, I can't call any of the applications since the python applications are not found in the normal directory.

Where can I set the PYTHONPATH to /mnt/sda1/.config/python3 in OpenWrt? Should I add export PYTHONPATH="${PYTHONPATH}:/mnt/sda1/.config/python3 to /etc/profile?

Sure. You could also add to the path within your python app before the imports. Finally, you could also soft link them in /usr/lib/python/site-packages/x so they "are" in PYTHONPATH.

One thing to keep in mind is that all the actions that are part of setup.py for these packages and were not just deploying the package have not happened (copying binaries...).

1 Like

Thanks. I added it to /etc/profile and reloaded bash but it doesn't work. The packages I installed with -t show up via pip3 list. However, they still don't run via the command line with "mnamer" or "python3 mnamer" python3: can't open file 'mnamer': [Errno 2] No such file or directory

I think I am missing something pretty basic here but not sure what. When you say copying binaries do I need to do that manually?

Just to be clear, if you are using bash (and not ash) and not using a --login option, you most likely need to have the PYTHONPATH in .bashrc or similar.
Also, to use mnamer directly, there needs to be the entrypoint in your PATH (which is the part where I wasn't sure about pip3 behaviour on -t as I haven't used it before)

Can you run a couple of commands just to give us a feel for your setup?

echo $PYTHONPATH
pip3 show mnamer
find / -name mnamer

The first one to see that PYTHONPATH is in fact set. The second one to check what pip3 did with the package and the third one to see if we can find the mnamer entrypoint.

Sorry, I meant to say ash. Here's the output. Seems like everything is fine?

echo $PYTHONPATH
:/mnt/sda1/.config/python3/
pip3 show mnamer
Name: mnamer
Version: 2.3.5
Summary: A media file organiser
Home-page: https://github.com/jkwill87/mnamer
Author: Jessy Williams
Author-email: jessy@jessywilliams.com
License: MIT
Location: /mnt/sda1/.config/python3
Requires: requests, requests-cache, guessit, teletype, appdirs
Required-by:
find / -name mnamer
/mnt/sda1/config/python3/mnamer
/mnt/sda1/config/python3/bin/mnamer
/mnt/sda1/.config/python3/mnamer
/mnt/sda1/.config/python3/bin/mnamer

For the last one, /mnt/sda1/config/ exists because I tried installing it to a non-hidden directory but it didn't work so I went back to the .config directory. I uninstalled the packages with pip3 but it didn't seem to remove those entry points.

thanks.
So, for you to call directly mnamer, it needs to be in your PATH.

You can first try directly with the full path to see if it works.
Does that work if you run it?

/mnt/sda1/.config/python3/bin/mnamer

Then the next thing is that python complained it couldn't find mnamer but that's because it was looking for that file locally when you run the command.

To see if it's in your PYTHONPATH, you can try the following:

python3
>>> from mnamer.__main__ import main
>>> main()
mnamer [preferences] [directives] target [targets ...]

But I'd say, you should just focus on being able to run the mnamer script from the bin/ directory (and forget about the python3 way, unless you want to extend the package with your own script, in that case, use the imports the way I did in that last example)

2 Likes

Great. Yes, calling the full path works, thank you!

I hear you on the script and will use that in extending the package for my own script.

But first, how do I make sure that I can call every bin from the command line? Do I need to change PYTHONPATH to PYTHONPATH="${PYTHONPATH}:/mnt/sda1/.config/python3/bin?

if calling

/mnt/sda1/.config/python3/bin/mnamer

works (if it doesn't, put the error in here).

Then what I meant was just to add that bin folder to the normal PATH environment variable which is used to find executable files. You can do that in your profile as well for example.

PATH=/mnt/sda1/.config/python3/bin:$PATH

Then you'll be able to call mnamer from anywhere.

2 Likes

Gotcha, I understand now. PYTHONPATH is for any python programs, while PATH is for ash. Duh. Thanks!

That fixed it.

1 Like

Back again. Something is really screwed up. I can create a new thread if needed, but basically I restarted the router and came back to mnamer not working.

I uninstalled all the python packages, reinstalled, and ran the following commands. mnamer isn't even showing up as installed.

pip3 install mnamer -t /mnt/sda1/.config/python3

**...**
****snipped****
**...**

Installing collected packages: chardet, urllib3, certifi, idna, requests, appdirs, teletype, babelfish, six, python-dateutil, rebulk, guessit, requests-cache, mnamer
  Running setup.py install for babelfish ... done
  Running setup.py install for rebulk ... done
  Running setup.py install for guessit ... done
Successfully installed appdirs-1.4.4 babelfish-0.5.5 certifi-2020.4.5.2 chardet-3.0.4 guessit-3.1.1 idna-2.9 mnamer-2.3.5 python-dateutil-2.8.1 rebulk-2.0.1 requests-2.23.0 requests-cache-0.5.2 six-1.15.0 teletype-1.1.0 urllib3-1.25.9
mnamer
Traceback (most recent call last):
  File "/mnt/sda1/.config/python3/bin/mnamer", line 6, in <module>
    from mnamer.__main__ import main
ModuleNotFoundError: No module named 'mnamer'
pip3 list
Package    Version
---------- -------
pip        20.1.1
setuptools 41.2.0

Give output of:
echo $PATH; echo $PYTHONPATH

echo $PATH; echo $PYTHONPATH
/mnt/sda1/.config/python3/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/sda1/.config/rclone
:/mnt/sda1/.config/python3/bin

OK, I can't see PYTHONPATH, give output of: echo $PYTHONPATH

Oh that's the second line.

echo $PYTHONPATH
:/mnt/sda1/.config/python3/bin

OK, I don't know, whether it is possible to start with semicolon. Please, run commands from python command line:

import os
os.environ['PYTHONPATH']
import mnamer
>>> import os
>>> os.environ['PYTHONPATH']
':/mnt/sda1/.config/python3/bin'
>>> import mnamer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mnamer'
>>>

OK, please, make following (change string above), and run commands again:
PYTHONPATH=/mnt/sda1/.config/python3/bin

Just to be clear, are you saying change line in /etc/profile from

export PYTHONPATH="${PYTHONPATH}:/mnt/sda1/.config/python3/bin"

to

export PYTHONPATH=/mnt/sda1/.config/python3/bin

Yes, exactly.

1 Like

Ok. I did that, reloaded /etc/profile, removed /mnt/sda1/.config/python3, and then reinstalled mnamer with pip3 to /mnt/sda1/.config/python3.

I'm getting the same error:

>>> import os
>>> os.environ['PYTHONPATH']
'/mnt/sda1/.config/python3/bin'
>>> import mnamer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mnamer'
>>>

pip3 also doesn't show any of the modules installed

pip3 list
Package    Version
---------- -------
pip        20.1.1
setuptools 41.2.0