Python script names in tasklist - python

I am wondering, is there a way to change the name of a script so that it is not called "python.exe" in the tasklist. The reason I am asking is that I am trying to make a batch file that run's a python script. I want the batch file to check to see if the script is already running. if the script is already running then the batch file will do nothing. Thanks

Maybe you can try this : http://code.google.com/p/procname/

This library does not work on Windows, and shouldn't be used in production code. Manipulation the argv array is a rather dirty hack.
Generally I'd not try to identify processes by scanning the process table. This is not really reliable, as process names aren't guaranteed to be unique. Instead I'd spawn a simple server on localhost inside the python script. If started, the script can then try to connect to the server, and quit, if the server is already running. This approach can later on also be expanded to support any kind of IPC.

You could use py2exe to convert the Python script to a .exe file which means you could then give it any name you like.
Alternatively you could use Python itself (rather than a .bat file) using the approaches given at Reading Command Line Arguments of Another Process (Win32 C code) to determine the name of the scripts being run by the 'python.exe' processes.

I'd simply create a lockfile in the local filesystem and exit if this exists already.

Copy python.exe to a file name of your choice.
C:\Python26>copy python.exe my_proc.exe
1 file(s) copied.
C:\Python26>my_proc.exe
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
In the tasklist it is showing as my_proc.exe.
I've tried to make a symlink of python.exe (mklink in Windows 7). Unfortunately it is still showing as python.exe in the task list.

Related

How to run code in the python interpreter from a python script?

I am trying to run code in the python interpreter from a python script (on windows, using the terminal build in to vsc), but I can't make anything work. I have spent a lot of time using the subprocess,and have also tried os module, but the issue with those, is that they cannot run code in the interpreter. So, I can make them start the interpreter, and I can enter code myself, which my script can get the result of (stdout and stderr), but it cannot enter code into the interpreter. I have tried running multiple commands in a row, using \n\r in the commands, and a few other attempts, but it always runs the second command/line after I manually quit() the interpreter. I have tried almost all of the functions from the subprocess module, and have tried numerous configrations for stdin, stdout, and stderr.
So, my qyuestion is: How can I have a script enter code into the interpreter?
It would also be nice to collect the results in real time, so my script does not have to start and quit an instance of the interpreter every time it wants the results, but that is not a priority.
Example of the issue with the OS module (but the issue is more or less the same with subprocess:
My code:
import os
print(os.popen("python").read())
print(os.popen("1 + 1").read())
Result:
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 + 2 #entered by me
>>> quit() #entered by me
3 #what the print statement returns
'1' is not recognized as an internal or external command,
operable program or batch file.
P.S. I am aware there is another question about this issue, but the only answer it has does not work for me. (When using the module they say, python cannot find the module after I installed it)
EDIT: my code with subprocess:
import subprocess as sp
c = sp.Popen("python", text=True, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
c.stdin.write("1 + 1")
c.stdin.close()
print(c.stdout.read())
Use the suprocess library like this.
import sys
import subprocess
p = subprocess.run(sys.executable, text=True, capture_output=True,
input='print(1+1)')
print(p.stdout)
print(p.stderr)
If you want to reuse a single child process, you have to implement a client and server system. One easy method is to implement a remote call with multiprocessing.Manager. See the example in the documentation.
As a side note, I don't recommend these if you don't have a good reason for spawning a child process, such as sandboxing an execution environment. Just use eval() in the parent process, because the child process will do the same work as what will be done by eval() if it has been done by the parent process.

Force Python to update command history file from command line?

I use Python without a GUI/IDE by issuing Python from the Bash command line. I use it within a Cygwin environment, which behaves like a Linux system in many respects.
The file used for the history of commands issued at the Python command line is stored in ~/.python_history. I can easily scoot in and yank content for manipulation using vim's Buffer Explorer. It's also easy to yank manipulated content into the system clipboard for pasting at the Python command line. (For more tactical revisions of commands, on the other hand, I just use readline to vim previous commands and a single-line basis.)
I have found that ~/.python_history doesn't update after each command. I'm not sure how often it is updated, but it's clear that exiting Python causes it to update. Putting into the background with Ctrl+Z does not.
(Is there a quick and convenient way from the Python command line to force an update to ~/.python_history?
Since my original posting of this question, I've had occasion to figure out Linux (Ubuntu) as a VirtualBox virtual machine. However, it seems very excessive to fire up an entire guest operating system just to be able to access the command history as a palette as opposed to one line at a time using the Up-Arrow key (or equivalently, k if one's command line editor is set to Vim). I'd even be happy with a Python counterpart to Bash's "fix command" (fc) command, even though I would have to erase all the lines that I don't want to execute.
Various things tried
As per the responses, I tried importing readline.write_history_file, but it isn't recognized, even though readline itself is:
$python
Python 3.8.10 (default, May 20 2021, 11:41:59)
[GCC 10.2.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
# Show modules
>>> import sys
>>> modulenames = set(sys.modules) & set(globals())
>>> allmodules = [sys.modules[name] for name in modulenames]
>>> print(allmodules)
[<module 'sys' (built-in)>]
# Fail to import readline.write_history_file
>>> import readline.write_history_file as whf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'readline.write_history_file'; 'readline' is not a package
# However, readline itself imports
>>> import readline
>>> readline.get_history_item(2)
'version'
readline does appear to be explicitly part of the Cygwin repository, so I didn't try pip install readline:
Search Cygwin packages for readline (followed by browser search for the string python)
The one result that appears to be most relevant shows dll,py, and pyc files with readline[s] in the file name, but I'm not sure what that says about the form taken by the readline module itself. I welcome clarifications/explanations about this.
In fact, I'm reluctant to try installing anything outside of the Cygwin package manager using pip for fear of creating inconsistencies.
Reading through this module, I found out readline.write_history_file(path) may be what you are searching for:
Save the history list to a readline history file, overwriting any existing file.
The default filename is ~/.history
First thing in the interactive interpreter, do
import readline.write_history_file as whf # write history file
Then you can do whf() or whf("path/to/.historyfile"), and your python history gets saved to disk immediately.
EDIT:
I'm not sure how often it is updated
It is only updated when you exit the interpreter. If you kill it, the history won't get saved, and Ctrl+Z just causes the interpreter to be suspended. If you then unsuspend it (by executing fg or bg, for example) and exit it normally, the history file will get written.

Not able to import my created module in python

Python is very first programming language i'm learning , i'm following "python programming by John Zelle". The problem is that after creating module Chaos i'm not able to import this module, it shows error message that " Import error: No module name chaos.
I can see the confusion. I have actually read this book as well when I was learning. First be sure you launch python in the same directory as Chaos.py and be sure to use a capital C if that is how you named it.
To import it in the python interpreter you would not type import Chaos.py just import Chaos and then call the main function as follows:
import Chaos
Chaos.main()
If you wanted to run the script in python you can just type the following from a command line in the same directory as the .py file:
python chaos.py
(These directions assume Python 3 on Windows. If your system is different then please edit the question to tell your system and version.) First create a file named chaos.py on your desktop. In the file, paste the following:
# File: chaos.py
def main():
print("This program illustrates a chaotic function")
x = float(input("Enter a number between 0 and 1:"))
for i in range(10):
x = 3.9*x*(1-x)
print(x)
if __name__ == "__main__":
main()
Next, to make this as easy as possible, we will use a modified IDLE shortcut:
Copy your current IDLE shortcut and paste it to your desktop to create a new shortcut.
Right-click on the new shortcut and click Properties.
Change the Start In box to %USERPROFILE%\Desktop. Click OK.
Now open IDLE by clicking on the new shortcut. Then enter:
import chaos
chaos.main()
There's an easy solution, the way I got it to work was instead of making the module perform inside a IDLE environment, I opened up sublime text(a program similar to notepad++), and I essentially wrote the code inside of there and saved it as a python file called "Chaos.py" inside of the folder where IDLE runs, which is :
(C:\Users\ngltm\AppData\Local\Programs\Python\Python36-32\) )
, the name next after the "Users" will be different for you, but I saved it inside of the Python36-32 file and after saving it inside of sublime text, I opened up a IDLE program and wrote "import Chaos" and it worked! The problem was the text in the beginning of every IDLE file that starts off with :
"Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information."
Well, I hope this helped!

Why do some Unix commands not work when called from inside Python? (command not found)

I often wish to perform Unix commands from inside Python, but I have found recently that some commands are not found. An example is the 'limit' command:
$ echo $SHELL
/bin/tcsh
$ limit vmemoryuse 1000m
$ python
Python 2.7.3 (default, Aug 3 2012, 20:09:51)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("echo $SHELL")
/bin/tcsh
0
>>> os.system("limit vmemoryuse 1000m")
sh: limit: command not found
32512
>>>
Another example is the 'setenv' command. Why do these commands do not work inside Python? I have tried using both the 'os' and 'subprocess' modules without success. Does anybody know of another module or method that will allow me to successfully call these commands from inside Python?
That's because some shell commands are not really programs, but internal shell commands.
The classical example is cd: if it were an external program it would change the current directory of the new process, not the one of the shell, so it cannot be an external program.
Roughly speaking there are two types of internal shell commands:
Commands that are implemented by the shell of efficiency's sake, but it still exists as an standalone program: true, false, test, sleep...
Commands that change the environment of the shell, and so cannot be done from a child process: cd, umask, setenv, ulimit...
The commands in the first category are quite shell specific. The commands in the second category, not so much.
For details see the man page of the relevant shell (man bash for example).
And if you want to know about an specific command run:
$ type -a <command>
Type is a bashism, I don't know the equivalent in tcsh, but which is an external program, so this:
$ which -a <command>
will show you whether your command exists as an external program, but it knows nothing about shell internals.
If you need the functionality of an internal command (of type 2 above) in your Python program you need to use the relevant system call. Hopefully it will already be available in some module. If not, you would need to write your own wrapper in C.
About your specific commands:
The environment (setenv and getenv) can be manipulated with os.environ or os.getenv, os.putenv, etc.
For the process limits (limit) take a look at the resource module.

How to start Python IDLE from python.exe window if possible, and if not, what is python.exe even used for?

I am running Windows 7 currently, and I remember when using Linux at the school computers I was able to type "gedit &" into the terminal for example to open up the gedit text editor. I was wondering whether there is a similar process to open IDLE, and for that matter a Python program/script by typing it into the "terminal-equivalent." I'm a complete newbie, so I may be off-base a bit...anyways, so there is this terminal-like program called python.exe, and it seems like it should be able to open Python-related software (like IDLE), and I was wondering 1) what python.exe is for, 2) whether it can be treated like a Linux terminal, and 3) how to do stuff in it. I've tried various commands and I get a syntax error for virtually everything. Much appreciated!
python.exe is the Python interpreter. All of your Python programs are executed with it. If you run it in the console, you will get an interactive prompt:
C:\> python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
If you give it a Python program, it will run it:
C:\> python myprog.py
You can also ask it to run an importable module:
C:\> python -m modulename
You can run IDLE this way:
C:\> python -m idlelib.idle
Yes you can start libraries like Idle, you need to import idlelib:
import idlelib.idle
Alternatively, to start idle from terminal without getting into python.exe shell first, you can do python.exe -m idlelib.idle assuming that idlelib is in your PYTHONPATH
The python.exe, is just like the shell in IDLE, is mainly used for quick experimentation with algorithms you want to try or testing library calls or language features, when you don't want to open a new file for that purpose. There are similarities with Linux shells like bash, but python's shell are heavily oriented for aiding programming while Linux shell are heavily oriented for starting up other programs. While you can start other programs in python's shell using subprocess.Popen and while bash do have it's own scripting language, they are very different.
python.exe is Python, the python interpreter specifically.

Categories

Resources