Raspberry pi call workon cv in bash or python script - python

I try to run a python script at start of raspberry pi. In the python script I use open cv so I need to call before:
source ~/.profile
workon cv
I try to write a bash file:
#!/bin/bash
echo "Bash script"
source ~/.profile
workon cv
#/usr/bin/python3 reco_eyes_from_video.py
python reco_eyes_from_video.py
But I get this error:
Traceback (most recent call last):
File "reco_eyes_from_video.py", line 1, in <module>
import cv2
ImportError: No module named 'cv2'
If I call myself:
source ~/.profile
workon cv
and then:
python reco_eyes_from_video.py
it is working fine
There is any way to run workon at the start?

It seems that you forget to write "python" before "reco_eyes_from_video.py" in your bash file.
To run this file at start, have a look at this answer

Related

Issue with Python 3 Imports when using a Shell script

I have the following scenario:
A Python3 Package (meross_iot) installed through pip and located in
~/.local/lib/python3.7/site-packages
A Python script (meross_electricity.py) that imports from this package: from meross_iot.controller.mixins.electricity import ElectricityMixin
A shell script (launcher.sh) that is meant to be a wrapper so that the .py script is run at startup:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/Documents
sudo python meross_electricity.py
cd /
If I simply execute the .py file everything works as expected, imports done, etc. If I try to run the .sh script I get the following error:
pi#home:~/Documents $ ./launcher.sh
Traceback (most recent call last):
File "meross_electricity.py", line 4, in <module>
from meross_iot.controller.mixins.electricity import ElectricityMixin
ModuleNotFoundError: No module named 'meross_iot'
Can someone please help me solve this issue?
Thanks!
Installed package with sudo worked .
Answer based on comment by #KlausD

Pyinstaller create an executable with external libraries

I am trying to create an executable file (exe) for a Python script that I written in PyCharm.
When I run the script from the PyCharm is working fine but when I try to run it as an individual .py or try to run the exe I am getting an error.
The issue I believe is with the "from infi.devicemanager import DeviceManager" library.
from infi.devicemanager import DeviceManager # Used for the retrievement of Device manager information
dm = DeviceManager()
dm.root.rescan()
devs = dm.all_devices # Get all the devices to a list called devs
for d in devs:
print (d.description)
I get following error:
PS C:\Python> python.exe .\tests.py
Traceback (most recent call last):
File ".\tests.py", line 1, in <module>
import infi.devicemanager # Used for the retrievement of Device manager information
ModuleNotFoundError: No module named 'infi'
PS C:\Python> pyinstaller -F .\tests.py
PS C:\Python\dist> .\tests.exe
Traceback (most recent call last):
File "tests.py", line 1, in <module>
ModuleNotFoundError: No module named 'infi'
[15072] Failed to execute script tests
Is there a way to include this library to the exe file so anyone without python can run this script?
I am open to suggestions.
Thank you in advance!
=== UPDATE ===
Full script can be found here
https://github.com/elessargr/k9-serial
My answer assumes that the interpreter that has infi.devicemanager is C:\Python\venv\Scripts\python.exe as stated by OP in the comments
So there is virtual environment named venv. You need to activate the virtual environment and install PyInstaller in it.
C:\Python>venv\Scripts\activate
(venv)C:\Python>
Note (venv) in front of your shell - that means virtual environment venv is activated
Now install PyInstaller
(venv)C:\Python>pip install pyinstaller
-- here it will install pyinstaller --
now you can test that both pyinstaller and infi.devicemanager are installed in venv
(venv)C:\Python>pip list
it should list both packages among some others. now
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager
--here it will create the exe --
if I was right it should work now
EDIT: It looks like infi is using __import__(pkg_resources), so the command should be
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager --hiden-import=pkg_resources

ImportError: No module named 'deployment'

Not sure whether I should be posting this on askubuntu or here.
I am trying to perform object detection using Tensorflow. I am therefor following this tutorial (this link shows the exact issue I am facing).
When running this script, I get this error:
$ python3 train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config
Traceback (most recent call last):
File "train.py", line 51, in <module>
from object_detection import trainer
File "/usr/local/lib/python3.5/dist-packages/object_detection-0.1-py3.5.egg/object_detection/trainer.py", line 33, in <module>
from deployment import model_deploy
ImportError: No module named 'deployment
According to the info I found online and the video I linked above I have to execute this command:
export PYTHONPATH=$PYTHONPATH:`pwd`/slim
While being in the tensorflow/model/ directory. Yet, it doesn't solve my issue. And I don't really know what I should check in order to find out what is causing this persistent issue.
Could someone help me out?
Thanks
EDIT:
exactly what I do:
(tensorflow) xxx#yyy-ThinkPad-X200:~/Downloads/models/research/build/lib/object_detection$ cd ../../../../
(tensorflow) xxx#yyy-ThinkPad-X200:~/Downloads/models$ export PYTHONPATH=$PYTHONPATH:`pwd`/slim
(tensorflow) xxx#yyy-ThinkPad-X200:~/Downloads/models$ cd ~/Downloads/models/research/build/lib/object_detection
(tensorflow) xxx#yyy-ThinkPad-X200:~/Downloads/models/research/build/lib/object_detection$ python3 train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config
Traceback (most recent call last):
File "train.py", line 51, in <module>
from object_detection import trainer
File "/usr/local/lib/python3.5/dist-packages/object_detection-0.1-py3.5.egg/object_detection/trainer.py", line 33, in <module>
from deployment import model_deploy
ImportError: No module named 'deployment'
(tensorflow) xxx#yyy-ThinkPad-X200:~/Downloads/models/research/build/lib/object_detection$
If you are installing from a clone of the tensorflow/models github you need to be inside models/research folder when you run the command:
$ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
Notice the extra `pwd` to your command.
I still had problems until I ran from within models/research:
$ python setup.py build
$ python setup.py install
$ python slim/setup.py build
$ python slim/setup.py install
Note: In your case you may need to run python3 instead of python.
This work for me:
Copy relevant deployment folder from the \models\slim directory
paste it to Python\Lib\site-packages directory
then run again.It will work!
It's PYTHONPATH not PYTHONPAT. You're missing H in your export statement.
As of now, the slim folder has been moved to models/research. So if models is in the /home directory, do the following to resolve the issue:
1- Open the ~/.bashrc and add the following line to the end of the file:
export PYTHONPATH="$PYTHONPATH:/home/models:/home/models/research/slim/"
3- Close the file and do source ~/.bashrc

Calling python script from a Bash script

I'm trying to call a python script from a bash script. I get import errors only if I try to run the .py from the bash script. If I run with python myscript.py everything is fine. This is my bash script:
while true; do
python script.py
echo "Restarting...";
sleep 3;
done
The error I get:
Traceback (most recent call last):
File "script.py", line 39, in <module>
from pokemongo_bot import logger
File "/Users/Paolo/Downloads/folder/t/__init__.py", line 4, in <module>
import googlemaps
ImportError: No module named googlemaps
There is more to this story that isn't in your question.
Your PYTHONPATH variable is getting confused somewhere along the way.
Insert a couple quick test lines:
in bash:
echo $PYTHONPATH
in your python:
import os
print os.environ["PYTHONPATH"]
At some point, the path to googlemaps got lost.
Your problem is in the script itself, your bash code is OK!. If you don't have problem running python scrip.py from bash directly, you should test if you use the same interpreter for both calls. You can check the shebang line in the python script, it is the first line in the file for example #!/usr/bin/env python or #!/usr/bin/python and compare it to the output of which python command if the output is different try to change or add the shebang line in to the file. If you call directly file in bash ./some_script.py bash reads the first line and if it is shebang line he wil execute the specific command for the file. My point is that if you use two diferent interpreters for calling file directly with python script.py and indirectly ./script.py one of them may not have the proper python modules.
Howto code:
$ which python
/usr/local/bin/python
So the second line is the path for your interpreter to build a shebang from it write in the first line of your script file this.
#!/usr/local/bin/python

How to set a python path when using runit

I am am using runit to manage a process on Ubuntu 12.04. I get the below error in the logs when I run:
sv up test/
I assume it is a python path issue.
ImportError: No module named htcommon.ht_redis
Traceback (most recent call last):
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py", line 17, in <module>
from htpData import HTPItemBase, HTPUserBase
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/htpData.py", line 9, in <module>
from htcommon.ht_redis import HTRedisConnection
ImportError: No module named htcommon.ht_redis]
I have also set the path in /etc/environment and also set in .bashrc.
Below is my runit script.
#!/bin/sh
exec 2>&1
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1
root#aws-rpc-server-east-staging-20130203070552:/etc/sv#
When I run the process from the command line I get no issues and it works.
/usr/bin/python /home/ubuntu/workspace/htFrontEnd/heythat/htanalytics/ht_rpc_server.py
Why will runit not work? Why can it not find the path?
Look like a few potential problems with this script. I don't think an 'export' can be 'exec'd. At least, it fails in my version of bourne. The exec command replaces the current process (your script) with the called command and doesn't return unless the called command fails. So, it doesn't make sense to 'exec' the exports anyway. Also they can be collapsed into one line. So, you're script should look more like this:
#!/bin/sh
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1

Categories

Resources