Password protect a Python Script that is Scheduled to run daily - python

I have a python script that is scheduled to run at a fixed time daily
If I am not around my colleague will be able to access my computer to run the script if there is any error with the windows task scheduler
I like to allow him to run my windows task scheduler but also to protect my source code in the script... is there any good way to do this, please?
(I have read methods to use C code to hide it but I am only familiar with Python)
Thank you

Compile the source to the .pyc bytecode, and then move the source somewhere inaccessible.
Open a terminal window in the directory containing your script
Run python -m py-compile <yourfile.py> (you should get a yourfile.pyc file)
Move <yourfile.py> somewhere secure
your script can now be run as python <yourfile.pyc>
Note that is is not necessarily secure as such - there are ways to decompile the bytecode - but it does obfuscate it, if that is your requirement.

Related

Issue with executing php through cmd from python

I will try to be brief!
For whatever reason, I couldn't make the entirety of my program in python, so I had to outsource one specific task to php (a language I do not know very well). As the python program runs, it is supposed to trigger the php program to run, and then do a few things afterwards which is not a problem.
It seems to me that, to be able to run something through python, you need it to be able to run through cmd first, and then you can make python use cmd to run the program. I had a few issues there, because the programs are on different drives, and the php program references other files and locations in the same directory and in sub-directories to where it is, this means I couldn't execute in one line of cmd, but first had to change directory, to then execute the php program from the folder it's in. Because my command wasn't just one line, I made a batch file containing all the steps.
My current working method is to open up cmd, change directory in cmd to where the php file is, and then run the php file. I had to add php to the "Environment Variable Path" to be able to do this. Here is the batch file that currently works when run by me:
cd /d C:
cd C:\Users\UserMain\Desktop\php\colorextract
php (2).php
When I double click this bat file, from my E drive, it successfully executes the php program. But when I tell python to execute the batch file, that is where things go wrong.
Here is my python code, apologies for the name of the bat file:
import os
os.system('cmd /k "bitch.bat"')
The resultant cmd window then goes thru the steps of the batch file: 1) it changes to the right directory, 2) it is unable to execute the php file because:
'php' is not recognised as an internal or external command, operable program or batch file.
Now, this is the standard error you get if you were to try running a php program without having added php to the "Environment Variable Path", I know this because I went through that same thing. But if I manually open a cmd window, not administrative or anything, I can 1) successfully perform the steps outlined in batch file, and program runs, and 2) I can even run the bat file, and that also runs the program.
The cmd window opened by python does not seem to be able to reference the "Environment Variable Path", or it is for another reason somehow handicapped against being able to do all the things that a normal cmd widow can. How can this be fixed?
Thanks in advance to anyone who reads this!
Edit: I found that python had not detected the changes I made to the environment variables the day before, hence why python's cmd was giving the exact error that not having php in the environment variable gives. After I restarted my computer, my code worked. Thank you to #Gerhard and #Aaron Junker for making me think much harder about this issue.
so I found a command that can be run after importing os.
print(os.environ)
I ran this, and it told me that Python could not see that php had been added to the environment variables, well, more likely that python did not have the most up to date information regarding what was in the path variable(s).
Restarting my computer made the changes kick in, and now my original code works. Whilst I do feel very stupid, I'm just happy that this is resolved.
This seems to me like both instances use different environment variables.
Open
System Properties -> Advanced -> environment variables and look that PHP is in the PATH variable in user variables and in System variables.

Is there a way to open Anaconda prompt, and insert a few commands that would run a python script, only by using VBA?

I am working on a project using VBA, and have found that the best way to perform a certain function within it is with a python script (Pulling data from a web service via a user-inputted serial number, and then populating info associated with that serial in an excel sheet)(Probably the WHOLE thing should've been done in python, but I am not familiar with it, and am with VBA somewhat, so this is the path that was chosen). I have had some success with this by using pyinstaller to make the .py into a .exe, however this caused other issues, such as the script not functioning properly, and taking a while to run.
It is simple enough to run the actual python script via the anaconda3 prompt window. However, this will be used by others, so I would like it to be automatic such that, they only need to click on a VBA macro button, which then automatically runs the python script.
Any thoughts on how this can be achieved?
Thank you!

Running Python Scripts Outside of IDLE

1- i want someone to help with this part
https://automatetheboringstuff.com/appendixb/
about Running Python Scripts Outside of IDLE and sending command line args .
2- i converted my code to .exe by pyinstaller ; what's the difference between this and running it
as a script .
3-how scripts are done . i see experienced people says :"i made a script to do something for me " how is that done >?
*i'm a beginner so try to make answers simple as possible.
If you're wondering about command line arguments, look into the "argparse" library https://docs.python.org/3/library/argparse.html.
The difference in the .exe and the normal script is that that the .exe versions (conventionally) are able to be redistributed to other sytems that don't have python installed whilst still being able to run the script. If you're only making the script for yourself the only real benefit is that you dont have to enter your IDE (code editor) every time you want to run the code, however if it's still in development then you'd have to compile the code every time you made a modification if you're wanting to run the code as an executable, which is very impractical.
Your third part is very, very vague. Python can be very versatile and i recommend you continue looking at the automatetheboringstuff.com website if you're interested in making scripts that can complete repetitive activities (although i must strongly advise you against using scripts maliciously).

Error while Registering the script to be run at start-up, how to resolve?

I am from electrical engineering and currently working on a project using UP-Board, I have attached LEDs, switch, Webcam, USB flash drive with it. I have created an executable script that I want to run at startup.
when I try to run the script in terminal using the code sudo /etc/init.d/testRun start it runs perfectly. Now when I write this command in terminal sudo update-rc.d testRun defaults to register the script to be run at startup it gives me the following error
insserv: warning: script 'testRun' missing LSB tags and overrides
Please guide me how to resolve this? I am from Electrical engineering background, so novice in this field of coding. Thanks a lot :)
The thing to remember is that you run the script as you but like chron startup does not, so you need to:
Ensure that the executable flags are set for all users and that it is in a directory that everybody has access to.
Use the absolute path for every thing, including the script.
Specify what to run it with, again with the absolute path.

How to make it extremely easy to run a python script for a user

I built a python/bash application for OSX that I want to distribute to non techsavvy users. My problem is how to get the user to initially run my python script. The script sets up a cron job so the user never has to do anything again but the user MUST run it the first time. The issue is that python and bash scripts default to non-executable when unzipped. As of now I have instructions in the readme.txt for the user to open the terminal and copy and paste the command which chmods the script and runs it for them. However is there anyway I can accomplish this without having the user open the Terminal? Ideally the user could just double click the python script and have it run. Thanks.
Sometimes being user-friendly warrants using platform-specific features. In this case, the Mac OS X "Installer" system: http://en.wikipedia.org/wiki/Installer_(OS_X)
Rather than a .zip file, you can create a .pkg file which users can download and install just as they would any other program native to OS X. This should give them a familiar experience, and you can make the .pkg file do what you need behind the scenes.

Categories