How to check if a script is already running - python-3.x

How do you check if your script is already running to prevent it from running/opening multiple times.
I am aiming for it to either quit and allow the script to open again or stop the new one from running.
Using:
Python 3.8
Windows 10
File type .pyw
Ide: Thonny
I have looked and haven't been able to find a answer.

Related

Subprocess.wait() in Python 2.7 vz Pthon 3.x

I'm just migrating my script from Python27 to Python 394 to run my build process like this:
p = subprocess.Popen(_make_clean, creationflags = subprocess.CREATE_NEW_CONSOLE)
p.wait()
p = subprocess.Popen(_make_rebuild, creationflags = subprocess.CREATE_NEW_CONSOLE)
p.wait()
Was working pretty nice in Python 27 but now I can see in Python 394 the Python script is not waiting for subprocesses. Of course, here it is necessary to wait until the first one (clean) is finished to start the second one (rebuild). I was able to make it work without the flag 'CREATE_NEW_CONSOLE'. Unfortunately, I need to separate the outputs for users to see what is the progress. It is just a mess if all scripts are running in the same console.
Why it is not working any more?
Ok, this is an interesting finding. I'm using ConEmu as my main terminal and it is fully integrated into Windows. In the past, I was NOT running my scripts over this emulated terminal and that seems to be the reason why I have this issue today. ConEmu is opening a new terminal window for each subprocess but in a different way than the native Windows Command-Line. When the script is called over Windows CMD everything is fine.

.exe doesn't run after building from .py [duplicate]

This question already exists:
Can't run script when i convert .py to .exe [closed]
Closed 3 years ago.
I am beginner at python. I have just created a script which includes a Tkinter GUI application. Whenever i run it using cmd it works fine but whenever i tried to create an exe of it using pyinstaller or cx_Freeze it won't run. It just pops up console and terminates within a second.
I think your problem is that using cmd, it starts the program and does not terminate until you close the shell, and the tkinter window stays open, but when you use an exe file, it terminates after all the code is run once, so you should probably have a while loop running preferably forever until the window is closed by you.

Python - execute sequential commands in same windows command prompt

I am very new to Python (been learning for a few days) . Can anyone help me with the following issue please?
I am trying to write a program to automate the testing of built in commands of a product which is installed on windows and linux.
Before any of the commands can run, the profile needs to be set using profile.cmd.
The commands ( command1.cmd, command2.cmd etc) then need to run in the same windows command prompt as profile.cmd as they rely on the environment which this sets.
The problem I find is that after profile.cmd has been run, python closes the windows command prompt and opens a new one for command1.cmd.
command1.cmd therefore fails to run properly as it is in a new command prompt which has not had the environment set by profile.cmd.
My code is shown below. I am currently testing on windows but will also need to test on linux.
Any suggestions would be much appreciated. Thanks - Kevin
import subprocess
rc = subprocess.run (["profile.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command1.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command2.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command3.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))

Is there a way we can change the working directory of Python (IDLE) permanently?

I am using Python Enthought Canopy. Each time I initiate the editor, I have to change the working directory by typing the code:
import os
os.chdir('C:/Users/Name/Canopy/scripts')
This is quite tedious as the directories are all reset after I quit the editor and start again.
Is there a way that I can change the default working directory, or change the profile of Canopy such that each time the settings will be run before coding?
1) Canopy's GUI is not IDLE.
2) Since this is ipython and has access to ipython magics, you can just type cd ~/Canopy/scripts with no import.
3) But I'm guessing that you have at least one script open in the editor. So you might prefer to just keep the directory synced to the editor, see http://docs.enthought.com/canopy/2.1/quick-start/code_editor.html#change-directory
4) All of the above said, yes you can run arbitrary code on ipython startup. See Ipython Notebook: Default Initialization Python Code

Open python IDLE in specific Windows directory?

Issue: IDLE always starts using a specific windows directory: 'C:\Python36'. That is my installation directory. I do not want to keep my project files in that directory. I want files for a specific project in a separate project directory.
After hours of searching, I did not find an answer to my specific question. I did find a helpful clue in How to start IDLE (Python editor) without using the shortcut on Windows Vista?
The solution that meets my needs is to create a file: idlehere.py
import idlelib.pyshell
idlelib.pyshell.main()
I then place that file in my project directory and execute the file with
python idlehere.py
This starts IDLE in my project directory.
Even though this works for me, I am posting this question in case it helps others; or in case there is a better way to accomplish my objective.
UPDATE: From the comment and with further experience I have learned additional ways to start IDLE in a specific Windows directory.
For each case open a command prompt in the desired directory.
At the command prompt, enter python. That starts python in interactive mode.
To start idle enter: import idlelib.idle
At the command prompt, enter python -m idlelib
At the command prompt, enter python -m idlelib.idle

Resources