Cannot schedule a python script to run through Windows Task Scheduler - python

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.

Related

How to enable shell autocompletion automatically after installing a python package

I'm writing a CLI application using the python click library, and I want users of my CLI to have shell autocompletion out of the box. Precisely, I want my users to install my CLI using pip install my-cli and after that they would have autocompletion already without having to source any shell file or install any other package.
I already implemented the autocompletion script as shown in the official docs of the click library and automated the autocompletion installation in setup.py. I did it following these instructions
However, in this case, my users would have to install my-cli and then re-source the shell script file (e.g .bashrc for bash).
Precisely, this is what is happening:
open the terminal
run pip install my-cli (I automated installing the completion script here)
users do not have autocompletion yet. Therefore, they should open a new terminal or run source .bashrc (this is the step that I want to automate, if possible)
After that, autocompletion works fine
I don't know if this is possible in my case. I know that I can't source a shell script using python since the python subprocess module will spawn a subprocess and the environment changes will not apply on the current process in this case (correct me if I'm wrong).
On the other way, some packages like git and docker provide autocompletion automatically after installing them. Last time I installed docker and then run docker <Tab> <Tab> I already got a list of the subcommands that I can use. So it would be interesting to know how to achieve this using python.
Interestingly, some other python CLI frameworks like typer provides a way to automatically install autocompletion using a subcommand. However, after running the install command, it shows a message that you should re-source the bash script or open a new terminal in order to get autocompletion, which makes me wonder if it's possible to automate this step, because I'm sure they would have done it if that is the case.

Run python script as non-root user

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.

Why can't I install my distribution into a local copy of python?

I've prepared a module that's ready to be uploaded to PyPI, the folder 'nester' has all the necessary components. Equipped with the build, dist, MANIFEST, and the two python files (setup.py and nester.py). Whenever I go to admin PowerShell I try running the final command to download it onto my local python file.
python.exe setup.py install
it spits back out 'permission denied', am I putting in the wrong command? the textbook I'm working from is working on terminal while I'm on Windows. The aforementioned textbook inputs
Sudo python3 setup.py install
and it seems to work perfectly for him 😒
here's what happens every time I try to run it
This likely happens, as the installer is trying to write into C:\Program Files directory. Ordinary user accounts do not have permission to do so, as the error Access Denied says.
/In *nix systems, sudo is used to act as root, and root can do anything. Thus permissions aren't an issue in the book sample.)
To illustrate your Windows case, see the image below. In the first case, ordinary user is trying to create test.txt into C:\Program Files. It fails, as expected. The second case does the same, but this time Command Prompt is started with administrative privileges.
As for a fix, you need to run the command in elevated command prompt. The prompt should have window title Administrator: Command Prompt, not C:\Windows\system32\cmd.exe.

Run python program from pip venv without system python

The answer to this question could be "You're as dumb as a wooden bowl" but I have searched a lot and haven't found a solution without installing python on other computers.
I have a python/flask web app that I need to distribute to many users. However, I can't install python on all those computers and there is no computer which everyone can access. And I cant serve the app internally from a server either. Yes, that's what I'm dealing with.
I have saved the git repo it in a network drive that everyone can access. I hoped I could run a batch file to spin the localhost server from a copied environment for the user and then use the web app.
I copied a conda environment over to the network drive and tried to use that but that gave me a Importing the numpy c-extensions failed error.
I tried including a pip environment (.\env) in the folder. So I thought any user could just activate the environment using the batch file ...
cd %cd%
.\env\Scripts\activate.bat
.\env\Scripts\python.exe run.py
but it's not working.
The .\env\Scripts\activate just crashes. I amended the activate.bat set "VIRTUAL_ENV=%cd%\env" to ensure it uses the current folder. Still crashes.
If I exclude that then .\env\Scripts\python.exe run.py still looks for a python installation at the path I have on my machine rather than the path I provided above.
Is there a solution to this?
All the computers will be using Windows but may vary between Windows 7 and Windows 10. I'm doing the development from my Windows 10 computer.
After activating venv My below code worked:
(Monday) C:\Users\Resurctova\Desktop\PoraPuski\Monday>python new.py
Output :
testing
since the new.py has code to print testing
As Monday is my venv I activated it and executed the script.
Do not execute in Scripts folder of your venv environment
Have you though about creating an exec file and that will save all your folders. Using a tool lik PyInstaller. You just need to share the output exe file without installing python.

Running Python Scripts with Jenkins

I am looking to schedule my python script runs with jenkins. The issue is, my scripts use a lot of libs like pandas etc that are installed on my mac terminal.
Is there a way to allow Jenkins to pick up these modules (or run the scripts as if it was terminal)? Also is there a way to run Python3 in jenkins?
I have already configured Jenkins to execute from custom workspace and have tried both shell and plugin executions.
The answer is yes, but it is detailed so I can only give you high level steps here. Jenkins can execute command line statements, and python modules can be run from the command line.
I would start by using the begins library to create a python file to run from the command line with arguments. Get it working on your local machine that way.
You will want to use either virtualenv or venv, and do all your pip installs using that virtual environment. Then you can copy the virtual environment to your Jenkins machine, or create a new one. Look into the freeze tool.
When calling your python from jenkins, you must first activate your virtual environment just as if you were working on it yourself.
You have a lot of research to do, but is very doable. I can help with follow up questions if needed.

Categories