0

I am working on a project using a Beaglebone running Debian and I have python modules and scripts in a directory structure like this:

/home/debian/product/test       (the scripts that I want to run)
/home/debian/product/src        (a bunch of custom modules that need to get sourced by the above scripts)

I have this at the top of a typical script, call it "/home/debian/product/test/myscript.py":

sys.path.insert(1, '../src/')
from mymodule import *
{the rest of the script}

(mymodule.py is located in /home/debian/product/src)

This works fine if I login to the device in an interactive SSH session. I can do this:

cd /home/debian/product/test
python3 myscript.py

and it works fine. But for normal operation I need to control the device in a non-interactive SSH mode, by sending commands from a Windows command prompt. From the Windows command prompt I run a non-interactive ssh session to execute the command:

"C:\Program Files\PuTTy\plink" -batch -l debian -pw password 11.1.1.20 /usr/bin/python3 /home/debian/product/test/myscript.py

and I get this error: ModuleNotFoundError: No module named 'mymodule'

If I replace the sys.path.insert with an absolute path, it works properly from the Windows command line:

sys.path.insert(1, '/home/debian/product/src/')

The problem is that I perform my development on my PC using an IDE and using the absolute paths prevents my IDE from seeing the location of my modules during development (before I send the code to the device).

Is there any way to get a relative path to work on my target device when using a non-interactive SSH interface?

Sanjo
  • 133
  • 14

1 Answers1

0

the reason is because /home/debian/product/test is not on your pythonpath, it's only there in interactive mode because it's in your current directory which gets added automatically to the pythonpath, so if you want to import it directly then you must manually add it to the pythonpath, otherwise you must use relative import using

from .mymodule import *

keep in mind that this will break your code when you run it in interactive mode from the same folder, so the better option is to just add its containing folder to your pythonpath using the next code somewhere in the top of your module.

import os 
import sys
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0,dir_path)

and a more general approach is to make a setup.py file that will permenantly add your folder to the pythonpath, but then you must structure your project correctly.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23