Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
window command window
#1
new with pthon

would like to have some python code to open window 10 command prompt window, do some practice code and then have some code to close the command prompt window.

found this but does not work. get error

start cmd /c python test ra1.py
Reply
#2
What's the purpose of the command window? Do you want to type into it? If so, why would you have the program shut it down? It won't know when you're done typing.

If you just want to run some windows programs, you don't need to open a command window to do so.

Right now it looks like you have a program (python? batch?) that tries to start a command shell and invoke a python program inside. I would suggest you run the python program instead of the command shell. Don't bother running it inside one.
ndc85430 and buran like this post
Reply
#3
Let me start again .
Currently using PowerBasic for windows for windows 10 programs, before that used PBCC for dos programs. Goal is to convert my windows programs to python and then to linux system.

Now when using PBCC (powerbasic console compiler) I would run a program in dos. It opens a console dialog box in the window 10 system.
Run the program there and when finished I would exit the program and the dos dialog box would close.

So want to:

Open the dos dialog box to run a program
Run python test programs there
And when finished , I would exit the program which would close the opened dialog window in window 10

Right now using Pycharm and the program run inside the IDE

I do not see an file upload here . Could send you an image of the dos dialog box running a PBCC program.

Maybe when a python program is create into an exe file it will do all that like the PBCC does? I do not know yet anything about making an exe file in python.

thanks for replying
Reply
#4
You could do that through the subprocess module
import subprocess as sp
sp.run(['cmd', '/c', 'python', 'file.py'])
(untested)
Reply
#5
create a python script in Pycharm or any other IDE/editor and save it as .py file

open cmd and run it e.g. python your_script.py.

converting the script to executable (e.g. exe on windows is different story and not needed for purpose of running the script in cmd)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
OK buran
Opened cmd prompt window, have a G: partition so type G: in cmd prompt. copied the .py file to G: root and it worked. Does not like spaces in file name.

Also found the code to clear the screen in cmd prompt.

Question , are there two type of exe's for python. One that would run in the dos type environment and one that would run in windows 10 desktop using an icon. May be a while before I attempt doing an exe.

below is a simple test code that I use in starting to learn python

import os
clear = lambda: os.system('cls')
clear()

i = 1
while i < 6:
    print(i)
    i += 1

aTuple = (10, 20, 30, 40)
a, b, c, d = aTuple
print(a)
print(b)
print(c)
print(d)
Yoriz write Aug-04-2021, 07:36 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#7
(Aug-04-2021, 02:56 PM)RobertAlvarez424 Wrote: Question , are there two type of exe's for python. One that would run in the dos type environment and one that would run in windows 10 desktop using an icon. May be a while before I attempt doing an exe.
No there is one .exe,can have multiply version(python 2.7, 3.8 or 3.9 but the name is still the same python.exe.
Windows see Python trough Environment Variables Path.
For install look at Python 3.9/3.8 and pip installation under Windows

So the version use most should be in Path,test both python and pip.
I use cmder if wonder about λ.
# Test python
G:\div_code
λ python -V
Python 3.9.5

# Path to .exe 
G:\div_code
λ python -c "import sys;print(sys.executable)"
C:\python39\python.exe

# Test pip
G:\div_code
λ pip -V
pip 21.1.3 from c:\python39\lib\site-packages\pip (python 3.9)
Quote:Does not like spaces in file name.
Use double quote if run command line in Windows.
# test code.py
import os

clear = lambda: os.system('cls')
clear()

i = 1
while i < 6:
    print(i)
    i += 1

aTuple = (10, 20, 30, 40)
a, b, c, d = aTuple
print(a)
print(b)
print(d)
So if run with this file name test code.py need to this.
G:\div_code
λ python "test code.py"

1  
2  
3  
4  
5  
10 
20 
40 

G:\div_code
Making .exe of this code with pyinstaller.
Have delete os.system('cls') and add last input("Press enter to Exit")
G:\div_code\dist
λ pyinstaller --onefile "test code.py"
112 INFO: PyInstaller: 4.5
112 INFO: Python: 3.9.5
190 INFO: Platform: Windows-10-10.0.19041-SP0
192 INFO: wrote G:\div_code\test code.spec
.....

G:\div_code
λ cd dist

G:\div_code\dist
λ ls
build/  dist/  'test code.exe'*  'test code.spec'

# Run exe
G:\div_code\dist
λ "test code.exe"
1
2
3
4
5
10
20
40
When make .exe should definitely not use space in file name because the from command line have to use "test code.exe",
this is confusing for end users of the .exe.

Quote:one that would run in windows 10 desktop using an icon.
Here comes file associations into the pictue,should point to main version as shown from command line in my case Python 3.9.
Reply
#8
ok everyone thanks, have a lot here tried.
Reply
#9
Gribouillis subprocess ?
Not quite sure how to use this? What I did do was make .py file using

import subprocess as sp
sp.run(['cmd', '/c', 'test_ra1.py'])

to a file ra_subprocess_test.py and then put this file with test_ra1.py in c: drive, open cmd prompt window.
Running ra_subprocess_test.py script runs test_ra1.py code which is the same a running test_ra1.py script.

--------------------------------------------------------------------------------------------------
snippsat

'No there is one .exe,can have multiply version(python 2.7, 3.8 or 3.9 but the name is still the same python.exe'

So this exe is for use in window 10 envoirment but actually can be a dos script or windows type script ? Opens cmd prompt window or used in window 10?

What is 'λ' character and how do you type this character with keyboard?
Do not now yet about pip. Does it package only dos type script?

--------------------------------------------------------------------------------------------------

Will be checking pyinstaller.
Reply
#10
Back to original question, suggest looking into pywinauto. Not sure it will do what you like but does handle a lot of automation.
See the docs here
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Minimize ADB window OomKoos 0 345 Dec-29-2023, 12:41 PM
Last Post: OomKoos
  add entries and labels to the window tkinter jacksfrustration 3 550 Oct-10-2023, 06:41 PM
Last Post: buran
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,613 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Can't stop keyboard listener to grab chars typed inside CTk window Valjean 9 1,254 Sep-25-2023, 08:07 PM
Last Post: deanhystad
  read active document name - other than from the window title ineuw 0 495 Sep-11-2023, 09:06 AM
Last Post: ineuw
  how to open a popup window in tkinter with entry,label and button lunacy90 1 811 Sep-01-2023, 12:07 AM
Last Post: lunacy90
Bug tkinter.TclError: bad window path name "!button" V1ber 2 725 Aug-14-2023, 02:46 PM
Last Post: V1ber
  Howto do motion event on solely window and not the widgets on it? janeik 3 792 Jul-11-2023, 12:10 AM
Last Post: deanhystad
  What is all the info in the info window in Idle? Pedroski55 3 647 Jul-08-2023, 11:26 AM
Last Post: DeaD_EyE
  Open a new window does not work Nietzsche 4 1,012 Jun-14-2023, 08:52 AM
Last Post: Nietzsche

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020