Run python script as non-root user - python

I installed python3 and required module with root access. But, When I try to run the scrip as non-root user. I am getting following error:
No module found Error.
What is the right way to run the python3 script as non-root user. virtualenv works fine If interactively runs it. But, I need to run it from nifi. So, I should be able to execute it without virtualenv.

You would need to install the module as non-root, or more specifically, the user account that runs NiFi.
You shouldn't be using sudo with pip anyway

The easiest way to do this would be to install Anaconda (big Python distribution with a nice installer) in a location accessible to NiFi and chown the Anaconda folder to the NiFi service account user.

Related

"Command not found" when trying to run a python cli application with "sudo" on Ubuntu

I have the following problem (I searched for hours but could not get it to work):
I have a python cli application (coded by myself). I created a fresh conda environment (with python 3.9.1) and installed myapp locally via pip install . into the env. All went well and I could successfully run myapp from the cli without any errors.
However, part of my application needs myapp to be called with sudo. By doing so, I received the error: Command not found: myapp.
My question is now: Why is this happening (Is this, because my env is somehow not correctly activated or "linked"?)?. I read, that sudo uses some other paths in its $PATH. But how can I fix this?
Any ideas are welcome ;)
Many thanks in advance
Edit: I also tried this using virtualenv but same result. Furthermore, I could successfully run the same app with sudo on my other system, so it must has something to do with my Ubuntu (18.04) system.

Cannot schedule a python script to run through Windows Task Scheduler

I have set up my windows task scheduler to create a task to run a python script that will send out an email to a few people (including myself).
I added the location the location of my Python in the actions tab in Program/script
C:\Users\User\AppData\Local\Programs\Python\Python37\python.exe
I added my python file as an argument
and the path to the file as Start in:
C:\Users\User\Documents\GitHub\automation
However when I run the script (I am testing it by running it every 5 minutes for 15 minutes). Python.exe opens up briefly but the emails in my python script are not sent, which means the script isn't running. I have tested my code several times and know that it works.
Most likely the task scheduler runs under a service account in Windows. This means it won’t have access to all the packages you installed locally. To remedy this, try
pip freeze —user > requirements.txt
Then in an admin command window:
pip install requirements.txt
This will reinstall packages so that they are available for all users
If you check the history for that task in your task scheduler, you'll see the exit code returned by the python interpreter. Most likely, this is either a permission issue (the user account you've configured the interpreter to run as does not have sufficient privileges to execute all the statements contained in the script) or a missing requirement. You mentioned in the comments that you were using an IDE; if that's the case, then you're likely using a virtual environment in your IDE and therefore the system installation of python is missing the package that you were using to run the code, leading to the emails not being sent out.
You can resolve any dependency errors by making sure to run pip install -r <requirements.txt file location> if you have a requirements.txt file specified in your IDE project, or you can simply manually install the required packages with similar pip commands. If the issue is Windows permissions, you can create a new Windows user with higher permissions (given that you have admin access on your own system) and have the task run as that user, or you can enable the Run with highest privileges check-button property in your task.

Run python Script as root

I have a python Script when i run my script by using my account it's run without problem ,
But when i use the root account i have an issue like this
I don't know why i have a problem with import module
use an absolute python path to point to the version of python that has the right packages installed.
The command which python executed as the user where the script works should help.
If you used pip to install any package locally, then you might have to
ensure as well, that the environment variable $HOME points to /home/robot.
In general I'd recommend to create a virtualenv foe your specific python project and use it in the sudo call explicitely or if you refer change the #! line in your script to point to the right virtualenv,

python script on azure web jobs - No module named request

I need to run the python scripts on Azure web jobs but i am getting the below error. I tried all the possible ways like scripts with virtualenv and append the path but none of them is working.
[10/08/2018 11:27:27 > ca6024: ERR ] ImportError: No module named request
Can you please help me to fix?
The script used in the file is,
import urllib.request
print('success')
according to
https://docs.python.org/2/library/urllib.html
you can check your python version. it's different between python2 and python3.
in python2.7, use :
urllib.urlopen()
instead of :
urllib.request.urlopen()
Please refer to below steps which I uploaded Python script into Webjobs previously.
1: Use the virtualenv component to create an independent Python runtime environment in your system.If you don't have it, just install it first with command pip install virtualenv
If you installed it successfully, you could see it in your python/Scripts file.
2 : Run the command to create independent Python runtime environment.
3: Then go into the created directory's Scripts folder and activate it (this step is important , don't miss it)
Please don't close this command window and use pip install <your libraryname> to download external libraries in this command window. Such as pip install request for you.
4:Keep the Sample.py uniformly compressed into a folder with the libs packages in the Libs/site-packages folder that you rely on.
5: Create webjob in Web app service and upload the zip file, then you could execute your Web Job and check the log
You could also refer to the SO thread :Options for running Python scripts in Azure

Sudoer statement syntax to source virtualenv activate file

I'm on a 64-bit Centos 5 Linux. This comes with Python2.4, but I needed 2.6 or higher to get psycopg2. I installed a virtual environment to run Python2.7.3 (based on this http://toomuchdata.com/2012/06/25/how-to-install-python-2-7-3-on-centos-6-2/) Now things seem to be working well when I'm logged in as root.
But I need to be able to use the virtualenv as a non-root user, joshmoe. (And start/stop postgresql as this other user)
The command for getting into the virtualenv is:
source /root/Python-2.7.3/projectdir/bin/activate
Understandably, when I run this command as joshmoe, I get a permission denied message:
-bash: /root/Python-2.7.3/projectdir/bin/activate: Permission denied
In an attempt to make this command available to joshmoe, I edited /etc/sudoers to include the following line:
joshmoe ALL=NOPASSWD: /root/Python-2.7.3/projectdir/bin/activate
Now from the joshmoe login, when I run:
sudo source /root/Python-2.7.3/projectdir/bin/activate
I get the following:
sudo: source: command not found
It seems pretty clear to me that I'm not using the sudoers file correctly to specify sourcing this file -- I seem to be giving access to that 'activate' file successfully, but not to the built-in source function. Either of following lines give syntax errors in sudoers:
joshmoe ALL=NOPASSWD: source
joshmoe ALL=NOPASSWD: source /root/Python-2.7.3/projectdir/bin/activate
I've found a good many examples of shudders statements. But I can't figure out the syntax that will make this work. I'm hoping once I get this one, I'll be able to give access to statements like 'service postgresql status' and so forth.
Your problem is that you user doesn't have root access to your virutalenv directory. You need to grant the user access for it to work, for example using:
sudo chmod o+x /root # grant access to root directory
sudo chmod o+rX /root/Python-2.7.3 -R # recursively grant access to everything in Python-2.7.3
source isn't a program, it's a shell butiltin which causes a script to be sourced in the current shell. sudo can only execute programs, not shell builtins, functions or aliases, that's why you get that error.
You can't run source as as sudo source; it's a built-in command for bash, not a program. What you want to do here is probably start a new shell as superuser using sudo -s.

Categories