Import Error in python when non root user - python

I am having an issue which has been driving me crazy. I am trying to run a python script as a non root user but when I try to execute the script I get the following error.
Traceback (most recent call last):
File "/usr/local/lib/EdgarRenderer/src/EdgarRenderer.py", line 13, in <module>
from arelle import PythonUtil # define 2.x or 3.x string types
ImportError: cannot import name PythonUtil
Now if I execute it as the root user it runs with out a hitch. I have triple checked all permissions and all the scripts and folders access by the desired user are in fact owned by that user (with the exception of the /usr/bin/python3.3 file). This is the command I am trying to execute
su - tomcat -c '/usr/bin/python3.3 /usr/local/lib/EdgarRenderer/src/EdgarRenderer.py -c /usr/local/lib/EdgarRenderer/conf/RunEdgar.xml --xdgConfigHome=/usr/local/lib/re3/arelle'
I run the exact same script the exact same way on another server with out any errors. I am using CentOS 6.5
Thanks!

Related

Applescript call python failed

I have developed an AppleScript which needs to call a python file. i.e autorun.py the Autorun.py start with
import msoffcrypto
import pathlib
import os
....
Both the AppleScript and the python file run fine. I even tried to call autorun.py in the terminal and that also runs with no problem. But when the Applescript tried to call the python file:
set myPythonScript to POSIX path of "/Users/zhouyu/Library/Application Scripts/com.apple.mail/autounlock.py"
set myVal to do shell script "python" & space & myPythonScript's quoted form
display dialog myVal
It failed at the first line in the python code when Applescript tried to call it.
error "Traceback (most recent call last):
File "/Users/zhouyu/Library/Application Scripts/com.apple.mail/autounlock.py", line 2, in
import msoffcrypto
ImportError: No module named msoffcrypto" number 1
Unlike Terminal.app, do shell script does not read your shell profile so make sure you give it the full path to your python interpreter, e.g.:
do shell script "/usr/local/bin/python3" & space & myPythonScript's quoted form

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

How to run a python program from python 3.8.0 IDLE shell

I want to launch the program Tip_and_Tax_Calculator.py from a folder named Python27 in a python 3.8.0 IDLE shell but I want to know which code will work. I have tried the exec function and execFile but they do not work, I don't know if I am using them in the wrong way too.
import Tip_and_Tax_Calculator
exec('C:\Python27\Tip_and_Tax_Calculator.py')
The error I get is:
Traceback (most recent call last): File "C:/Users/Jayan
Subramanian/AppData/Local/Programs/Python/Python38-32/Project
Launcher.py", line 1, in import Tip_and_Tax_Calculator
ModuleNotFoundError: No module named 'Tip_and_Tax_Calculator
you could use the exec function combined with open and read in the following way for python 3.x:
Notice I change from \ to /:
exec(open('C:/Python27/Tip_and_Tax_Calculator.py').read())
The method you tried and specified is relevant for python 2.x.
Another issue, you do not need to import the Tip_and_Tax_Calculator
EDIT
after the user added the error message the problem that the script didn't run is the fact the module Tip_and_Tax_Calculator is not in the current shell directory, so one need to specify its full/relative path

Error when running a python script

I am trying to run a script using an interface created with tkinter. I have a button that executes a script which code is:
subprocess.call("python3 " + PATH_TO_SCRIPTS + "main.py 1 &", shell=True)
However, when this button is pressed I am getting the following error.
Traceback (most recent call last):
File "/home/m//PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/main.py", line 17, in <module>
from controller import *
File "/home/m/PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/controller.py", line 5, in <module>
import rospy
It says that the module rospy does not exist, but when I run
import rospy
using python or python3 it is imported successfully. What can I do to solve this issue? I am using Ubuntu.
The comments to your question are mostly about Python, but I guess it is more of a ROS issue.
You don't have to set-up your PYTHONPATH manually to find rospy but you have to source the setup.bash of your catkin workspace (otherwise none of the ROS tools is found).
Usually this is done by adding something like
source ~/catkin_ws/devel/setup.bash
to .bashrc. This works fine for everything that is run in a terminal.
I don't know how you start your script but as it provides a graphical interface you probably just run it by double-clicking it in the file browser? If you indeed do so, the script is not run in a terminal and therefore can't find the ROS modules. Run the script from a terminal (in which the setup.bash has been sourced) and it should work.

A crontab script accessing Django models fails with ImportError

I'm trying to use a crontab to execute a python script periodically. I followed the solution given here to execute the command using virtualenv. Following is my crontab
SHELL=/bin/bash
HOME=/
MAILTO="myid#example.com"
* * * * * cd /home/jaskaran/edmhunters && /home/jaskaran/edmhunters/env/bin/python /home/jaskaran/edmhunters/scripts/db/songlist.py
I keep getting this ImportError
Traceback (most recent call last):
File "/home/jaskaran/edmhunters/scripts/db/songlist.py", line 4, in <module>
from hunt.models import DJ, Song
ImportError: No module named hunt.models
The script works fine when run from the shell. What am I missing?
Django import errors are often misleading due to how Django loads its INSTALLED_APPLICATIONS and all implicit code what comes when Django tries to access its settings.
To work around, create a Django management command out of your script.
Then try this syntax if you need to run virtualenv'ed Django script or management command from shell script or cron:
export DJANGO_SETTINGS_MODULE=mymodule.settings ; /srv/django/myproject/venv/bin/python /srv/django/myproject/manage.py MyManagementCommand

Categories

Resources