Python Forum

Full Version: window command window
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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.
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
You could do that through the subprocess module
import subprocess as sp
sp.run(['cmd', '/c', 'python', 'file.py'])
(untested)
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)
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)
(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.
ok everyone thanks, have a lot here tried.
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.
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
Pages: 1 2