Issue with Python 3 Imports when using a Shell script - python

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

Related

Imported module cannot be found when Python script is run via a bat file

I know there has been similar problems, but unfortunately most of them are related to errors with pyperclip itself instead of the batch file, which i suspect is where the problem stems from.
Below is an MRE of my Python script:
#! python3 -> Do I have to use my version(3.8)?
# pw.py - An insecure password locker program.
import sys, pyperclip
#do something with the module
And my batch file pw.bat:
#py.exe C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py %*
#pause
I am running python 3.8 on windows 10. I imported the pyperclip module in my python script pw.py and ran the file via pw.bat, and this in turn gives me this error:
Traceback (most recent call last):
File "C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py", line 7, in <module>
import sys, pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Press any key to continue . . .
Which shouldn't happen as I have installed pyperclip on the project using pip, and the script itself runs just fine in pycharm. What am I missing?
EDIT: I forgot to mention that I am using pycharm. So the thing is that pycharm had also installed python.exe in the project folder. And as the module pyperclip is only installed to that folder, the python.exe used in the bat must point to the one in the project folder.
i don't know why are you using py.exe. when running commands from a batch file or cmd .you should use python.exe.obviously you would need to add python to add for doing so.instead of adding py.exe to path,add python in system variable Path which is somewhere present in C:\Users\[username]\AppData\Local\Programs\Python\(your path might be diffrernt).you can add python in Path by following this post
after adding python to path just use the following batch-file:
#echo off
python path-to-your-py-file\filename.py

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

Cannot run a python script using crontab

I am struggling to run the a python script as a cron job.
I am logged in as root
the permission for the python script is
-rwxr-xr-x 1 root root 2374 Mar 1 22:49 k_collab_spark.2.py
I am starting the script with
#!/usr/bin/env python
I tested the pythong script
if i do "./k_collab_spark.2.py` this work fine.
on the crontab i have set the job as
15 12 * * * /opt/lampp/htdocs/testme/SPARK/k_collab_spark.2.py >> /var/log/kspark.log
I do not see any message on the log file
Once i adde 2>&1 it gives an error Traceback (most recent call last):
File "/opt/lampp/htdocs/kabeer/SPARK/k_collab_spark.2.py", line 2, in
import requests
ImportError: No module named requests but if i execute the service manually it is successful . WHen i run it manually it works fine
Tried defining the path but still the same issue
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
import requests
ImportError: No module named requests
Any idea what i am missing.. Appreciate any help around this.
Try to run script with another first line:
#!/usr/bin/python
If it's executes successfully the problem in python interpreter, because when you have several versions of Python installed, /usr/bin/env will ensure the interpreter used - is the first one on your environment's $PATH, which i guess has no requests lib.
Can you add python explicitly before the script name?
At the end of the crontab line, add 2>&1, which redirects error messages to the log file as well. See this link for a detailed description In the shell, what does " 2>&1 " mean?
There is also a possibility that your current user and root runs different versions of python.
I used a shell script to call the python script. THe anaconda on the box was causing the trouble
export PATH=/opt/anaconda3/bin:$PATH
/opt/anaconda3/bin/python /opt/lampp/htdocs/scriptme.py >/opt/lampp/htdocs/scriptme.log 2>&1
Add the following lines of code to your script and edit the crontab :
from distutils.sysconfig import get_python_lib
print(get_python_lib())
Now check the log in crontab, you will get some path
e.g. "/usr/lib/python2.7/dist-packages"
cd(change directory) to the above path and ls(list directory) to check if package exists ; if not :
sudo pip3 install requests -t . # dot indicates current directory
or else if you have a requirements.txt file then you could try:
sudo pip3 install -r requirements.txt -t "/usr/lib/python2.7/dist-packages"
#try this from the directory where "requirements.txt" file exists
Now run your scripts.

importError using a virtualenv from cmd on Windows

trying to run a flask dev server on localhost:5000 using a virtualenv on Windows 7
In my command line in the project directory, i activated the virtualenv with the command "env\scripts\activate". It seemed to work, as my next line was preceded with an (env) tag. When I attempted to run the app file (bank_app), however, I got an import error.
Here's the console log
C:\Users\TJ\Documents\Python Projects\TestingPython> env\scripts\activate
(env) C:\Users\TJ\Documents\Python Projects\TestingPython> bank_app
Traceback (most recent call last):
File "C:\Users\TJ\Documents\Python Projects\TestingPython\bank_app.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
and here's a gist of the bank_app file (just in case it's relevant)
I'm used to running the code directly from PyCharm, which handles the virtualenv for me. it works fine running directly from PyCharm
This is probably related to how Windows maps extensions to executables.
You started the script with bank_app, which is really not the name of your script (your script has a .py extension I assume?). Windows must be doing a search in your directory, and then starting the script with the interpreter that is registered for the .py extension of the script, which is the globally installed interpreter, not the interpreter that is currently in the PATH.
I'm pretty sure if you run the command as python bank_app.py everything will work just fine.

Categories

Resources