Not Importing modules on autostart of python program

So, let me give a little background on what I’m trying to achieve and where exactly this error is coming.

So, my aim is: As soon as I turn on my Jetson nano my python program would start running. The python program doing object detection and uploading the number of objects in the frame to realtime database on firebase (which in turn we use to display on app). So the important thing is the module firebase.
So I install firebase using the command:

pip3 install python-firebase

and inside the code we importing these:

from firebase import firebase
from firebase.firebase import FirebaseApplication

So here is where things get interesting, when I run this program through terminal like:

python3 myscript.py

it runs without any problem but as I mentioned above earlier that my aim is to run that program as soon as the jetson gets plugged in, so I’m achieving that like this( i ran a demo script by this method and it works):

So this is where the problem comes, when it tries to import all the modules, the process gets exited and when I logged that error it shows (as I can’t see the terminal when this program autostarts):

No module named firebase

It works perfectly fine when executing the script manually but this is the problem I suppose…Can anyone help me out here? I leave the dummy code below to try this.

EDIT: changed title

Here is the dummy code if want to try it out yourself:

import cv2
import numpy as np
try:
    from firebase import firebase
    from firebase.firebase import FirebaseApplication
except Exception as error:
    f=open("error.txt","a")
    f.write(str(error))
    f.close()



cap=cv2.VideoCapture(0)


#establishing connnection with the database for the first time

while True:
    try:
        #firebase_message = firebase.FirebaseApplication("<link to the database>", None)    
        break
    except:
        continue
print("Connectionion Established")

while(cap.isOpened()):
    _,frame=cap.read()

   
   # cv2.imshow('frame',result)
    k = cv2.waitKey(1) & 0xff
    if k == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Use this to run your python program on boot

please test this dummy code and share if you have a solution to this issue

The first import overwrites the name “firebase” so the second import wouldn’t work. You might succeed if you instead do the second line as from firebase import FirebaseApplication

Here’s how samples on the web use this module:

from firebase import firebase
firebase = firebase.FirebaseApplication('https://your_storage.firebaseio.com', None)
1 Like

hello @snarky , I tried what u suggested but the problem is coming on the first import.
And this error occurs when this program runs as soon as I plug in the power in jetson. If I login (through ssh) and then manually execute the script, then there is no issue. It runs smoothly. Any idea why it is not importing?

I think you should try to change the user as root.

sudo su

You should try to execute your code in root mode. If your application does not run, you should install your python packages in root user.
You can check the packages with these commands.

sudo su
pip3 list
pip3 list | grep

well, I checked the root user pip3 list, and the firebase module wasn’t there…so I did
pip3 install python-firebase , rebooted the jetson but still same error gets logged…
No module named firebase
I also check which pip3 jetson was using in both cases and in both the cases it was:

/usr/local/bin/pip3

Maybe if you can run the dummy code yourself then you might be able to figure out why it is not importing the module…
Note: every time when I manually execute the script, it works, the problem occurs when it autostarts on reboot

Hello @Prithviraj
I just tried your dummy code with the autoscript, it works for me on autostart. I used Xavier NX with my custom board and used JetPack 4.5.
I chose the “autologin” option instead of “require password” option while installing JetPack.

hey @ozguryildiz , I didn’t get that option while when I installed jetpack (I specially formatted my sd card and installed jetpack again to check that) …But is that the problem? because if I comment the firebase part of the code then on autostart it works fine…it seems like the problem occurs only on the firebase modules…i havent checked for any other…

okay there is something weird happened…so I imported tensorflow and matplotlib.pyplot to check if the problem is the firebase module or is it other modules as well so… turns out on autostart it logged

No module named tensorflow
No module named matplotlib.pyplot

My guess is, except the default libraries that come with python (in this case: cv2,numpy) it will show the error on autostart for these libraries…
So if this is the problem then we have to figure out why it is not importing these libraries? Probably it needs to autologin in my user account? @snarky @ozguryildiz

okay guys, I figured it out…It was a silly error. All of my dependencies were in python3 and I was trying to run the code through python. Anyways thanks @ozguryildiz @snarky .

Also, I more thing, I want to execute this command first before I execute my script…
export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libgomp.so.1
So in turn, I’m executing two commands.
So what should changes should I make in ~/.config/autostart/myscript.desktop?

[Desktop Entry]
Encoding=UTF-8
Name=myscript
Comment=myscript
Icon=gnome-info
Exec=export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libgomp.so.1 && python3 /your/path/script.py
Terminal=false
Type=Application
Categories=

X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Delay=0

1 Like

hello,
I achieved the same using

[Desktop Entry]
Encoding=UTF-8
Name=myscript
Comment=myscript
Icon=gnome-info
Exec="export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libgomp.so.1 ; python3 /your/path/script.py"
Terminal=false
Type=Application
Categories=

X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Delay=0

Still, thank you for your help and time, I appreciate a lot.

2 Likes