I have a function in python, that I sometimes run manually and sometimes run automatically with windows task schedualer. I want some functionality to be different depending on the way it is run. Can I do something within the function that would tell me how it was run?
The reason I need to know that, is that when I run it manually, I want to double-check with the user (e.g me) that I want to proceed with some data over-ridding, but when it is run automatically, no-one would be there to confirm (and as it is done automatically, it was probably done right).
UPDATE:
My problem was solved in a different way, as the automatic scripts are concentrated in a special path, so I can use os.getcwd() to know if the script is run from that path, which means it was run automatically.
Related
Obviously, the same program is run, and the code is the same. Why do two of them appear the same
When you execute scripts with the same name but different file locations or run configurations this can happen.
It can be useful in a variety of situations; where you want to run the same script in multiple different versions of python. Each Run configuration can be setup to use a separate interpreter. Though, it would be wise to rename the run configuration.
In this case, pycharm simply took the name of the script and appended (1) when it saw another run configuration of that same name already existed.
I have a short script written in python that saves a simple .txt to the same directory the .py file is in. Problem is: It only does so on one of my two computers.
My code doesn't include a hard-coded path to write to. On my laptop, I can put the makemeanote.py in any folder and it will create the note right there. On my desktop pc, all the notes end up in System32. PATH is set exactly the same way on both machines, and both use Windows\py.exe as the executable.
Somewhat interestingly, I only get an admin-screen on the desktop pc, asking if I want to allow changes to my system by "Built: Release_master_v3.8.2_"etc., whereas on my laptop it simply runs and does its job.
No amount of un- and reinstalling has changed anything, even when I thought I had eradicated any trace of python on my hard drive. Both PCs use current Win10/64 installations.
What is happening there?
A clarification: It's not about fixing the bug, it's about understanding the inconsistent behaviour! I know I could just hard-code any directory, but that takes away the beauty, don't you think?
I finally found the cause of the weird behavior:
For some reason, the py.exe in my WINDOWS folder was set to always be run as admin. As soon as I unchecked that option, the User Account Control (UAC) check disappeared and my file finally behaved as I had expected.
I am not sure what I am supposed to be searching, because everything I have been trying to look up has not really given me the answers that I need.
If anyone can point me in the right direction of exactly what I should be looking at, I would greatly appreciate it. Basically, I want to know the best way to go about making a python program which I can distribute (not publicly).
Once someone has the file they run a script, or run the make command, and the result of this would be that the user can invoke the program from within any directory on the computer by just typing the name (and passing args) in the terminal.
I only know of one way to do this, which I believe is the wrong way. My idea was something as follows:
1.) A main folder, let's call it Ghoul. It would contain 3 files:
setup.sh
ghoul.sh
ghoul.py
2.) The user would have to run setup.sh, which would make ghoul.sh an executable and it would change the name to ghoul and then I would move it to /bin
3.) Within the ghoul file which is now in /bin, the file itself would contain "python /home/user/Ghoul/ghoul.py $#" And this would allow the program to run from any directory just by typing "ghoul"
I hope it's clear what I am trying to accomplish, I know this can't be the appropriate way of doing this. What should I be looking up and reading on to accomplish this? I would like to know how I can do this on Ubuntu and if also ..Windows.
I am also interested if I can limit the "ghoul" command to work only in the Ghoul directory, but of course in any directory that is within the Ghoul directory instead of from within any directory on the system.
Thank you.
On Unix like systems
#! /usr/bin/env python
in the very first line of your chmod +x script file will make it executable from the current directory using ./filename
If you want completely straightforward execution from anywhere you can put it on the path somewhere however you choose.
On windows its more complicated, if you want commandline execution the default install should give you the ability to execute
myscript.py
If its in your path. to drop the .py you need to get .py into the PATHEXT variable. Alternatively if it neds to be distributed to someone without any python install check out www.py2exe.org
I'm having the same problem on Windows and Linux. I launch any of various python 2.6 shells and run nose.py() to run my test suite. It works fine. However, the second time I run it, and every time thereafter I get exactly the same output, no matter how I change code or test files. My guess is that it's holding onto file references somehow, but even deleting the *.pyc files, I can never get the output of nose.run() to change until I restart the shell, or open another one, whereupon the problem starts again on the second run. I've tried both del nose and reload(nose) to no avail.
Solved* it with some outside help. I wouldn't consider this the proper solution, but by searching through sys.modules for all of my test_modules (which point to *.pyc files) and deling them, nose finally recognizes changes again. I'll have to delete them before each nose.run() call. These must be in-memory versions of the pyc files, as simply deleting them out in the shell wasn't doing it. Good enough for now.
Edit:
*Apparently I didn't entirely solve it. It does seem to work for a bit, and then all of a sudden it won't anymore, and I have to restart my shell. Now I'm even more confused.
I really love python because I love interactive development. There's one area where python appears to fall short, however, and that's in the area of automatically reloading changed files. Basically, what I want to have happen is to be able to modify a python file on-disk and then have my running python instance automatically reload the changed module to allow me to immediately access my changes in the REPL so I can test them out. Basically, I want some sort of watch command.
I happen to use the bpython shell because I think it's the best one available, but this feature is so important to me that I'd be willing to switch to any other python shell that does it right. Is it possible?
Something like tail -f in python + reload().
I really think they should make the tags OS and Python version mandatory though.
If you're trying to "test out" your code, perhaps you should be looking into doing automated unit tests instead of testing your code repeatedly and manually. It'll allow you to test more code quicker and waste less of your precious, precious development time.
Personally, I use unittest with py.test as the runner.