Python on Windows opens new window - python

Here's the problem, after installing Python (3.6, 3.7, 3.8) on Microsoft Windows when you invoke it, it opens in a new window.
This question has been raised before, and replies talk about modifying the code to pause the output or keep the program running so it doesn't close the window. I can not modify every python package ever made for windows compatability.
My problem is that this is not how Python works on *nix platforms. Surely there must be a way to get python to execute within a command prompt ?
My immediate issue is that I loose the console output on so many python programs. From Jupyter Notebook to AWS GRC (remote-codecommit).
-=-=-=-=-= ANSWER =-=-=-=-=-
After much frustration, it turns out the problem was related to account privileges.
The behaviour above occurred when a user with elevated rights executed Python.
When I log out and run with another user that is a local admin, it now behaves normally.
I cant tell you the exact difference between elevated rights and local admin, but there is something in the permissioning that effects how Python is run on Windows.

This happened to me when the current directory of the terminal didn't exist (was deleted/renamed after the terminal was started).
Solution: cd to some directory.

Find you python folder and select python.exe and create a shortcut for it.
Example Image
There's probably a better way, but this is a simple workaround.

Related

VS Code trying to run .../Activat.ps1 in cmd terminal

I'm using VS Code for a Python project using a virtualenv. I switched my deafult terminal from powershell to cmd as VS Code was not happy executing powershell scripts.
Now when I open a terminal in my project it opens cmd (as desired), but automatically tries tor run .../Scripts/Activate.ps1, which it doesn't like. I want it to run .../Scripts/Activate.bat as we are in cmd. Runnning it manually for now, but would be nice if I didn't have to.
No doubt there is a setting somewhere to change this, but I cannot find it. Any ideas?
This is a problem related to the Python extension, it should be fixed in the last update.
You can get some information from here.

Python in VSCode: How do you run a previous command?

I'm kind of new to software development. Outside of VSCode, I can open up a terminal (let's say PowerShell), run python in it, type in a command (like 2+2), be able to click the up arrow key to find my previous command so that I can run it again.
If I run PowerShell in VSCode and do the same thing, nothing happens when I click the up arrow where I would expect my previous command to be cycled.
Is this a problem with my Python or VSCode? I've been looking for a solution for this but haven't found many useful topics on this.
This was going to just be a comment but its too long. Sorry it isn't more informative than it is. This is kind of an odd problem because VSCode isn't a true IDE. It doesn't have its own shell and just hijacks your powershell or bash terminal, depending on which OS you are using. You should be able to use your up and down arrows just like you can in powershell. I have tested it on my own VSCode installation and it works fine for me. If it's a problem, it's not with python, since VSCode will interact with the terminal the same way no matter which language you are using it for, so its probably with VSCode or your terminal. I have heard of others having issues with up arrow autocomplete in bash, so if you are connecting to a bash terminal that could be it, but I've never heard of it glitching in powershell. I'd say check which terminal you are using, see if the problem persists when you change terminals, and try reinstalling VSCode if it does. Past that, I don't know what to tell you.
I found a work-around. For me, neither git bash, nor PowerShell allowed up/down arrows for history switching within a python shell. So here it goes.
Ctr-Shift-P opens VSCode commands
Python: Create Terminal does not actuallt start python, but it does launch powershell in a mode that will enable us to succeed
py starts python shell with working up/down arrows!

Run a Python script when a program starts

I would like to run a simple Python script when a program starts (like Chrome). In Windows and ubuntu.
https://www.youtube.com/watch?v=8BiOPBsXh0g&t=235s
There you can find tutorial to hide auto running program when someone run application on Windows :).
At ubuntu that is a little harder, u can create bash script for example... But any script can be revealed, and depends on visual interface, method to hide it can fail.
Otherwise is a method to edit .bashrc in home directory with some similar to:
alias COMMAND_DEFAULT_RUNING_PROGRAM='THAT_SAME_AS_LEFT;python3 your_script.py'
But that will be visible on aliases. I don't found any effective method to hide it :)

In Visual Studio Code, how do I load my python code to a read-print-eval loop?

I am teaching a class that uses VScode.
I am used to teaching using IDLE, and it is very nice for the students to be able to call their defined functions and run snippets of code in a python terminal, for debugging purposes.
In VScode, they I have been unable to do the same in a satisfactory way.
Option1: I can select all code, right click and run selection/line on terminal. This works for small snippets, but I cannot do it for the whole file (even after selecting the whole file with ctrl-A). On linux, this works, but on windows, it does not, unfortunately (and my students use windows)
Option2: I can use the debug console. This requires adding a breakpoint in one of the last lines of the file, and does not offer tab completion. It works, but is less convenient than IDLE.
Option 3: I can also add the commands to run to the bottom of the file (which is a least preferred alternative, given that is forgoes the interativity of the read-print-eval loop).
Is there any better solution? Installing a VScode extension would not be a problem.
Visual Code is just a text editor like your traditional notepad. to run and debug any kind program you need to install the particular extension for the programming language.
In your case you are using python so you need to install the extension of it. the best one is the "Python" which is developed by microsoft itself. go to your extensions manager and install this extension. right click and click "run python file in terminal" and you are all set.
this will run exactly as they run from the idle(which is default IDE provided by python itself) you can enter the arguments from the console itself. according to me this is the best way to run and debug python programs in VScode.
another way is that VScode shows which python version is installed on your computer on the left bottom side, click on it and the programs will use this interpreter.
out of all the ways listed here and many others, the best method is to run the program in the terminal which is the recommend by python itself and many other programmers.
this method is very simple. what you have to do is open up your command prompt and type the path where python.exe is installed and the type the path of the your program as the argument and press enter. you are done !
ex : C:\Python27\python.exe C:\Users\Username\Desktop\my_python_script.py
You can also pass your arguments of your program in the command prompt itself.
if you do not want to type all this and then just use the solution mentioned above.
hope that your query is solved.
regards

python console closes before it starts

I am learning Python and came across this problem. I know that i can put "input()" at the bottom of the program, but the problem is that it IS there, but console terminates nevertheless. It seems like the programm never starts at all. I am using the 3.5 version of Python.
Open a terminal, navigate to the directory of your file, and type python myfile.py. If you just double click the file in your system explorer it will run and close when it finishes, which is often too fast to see what's going on.
I think you are starting the programme by clicking on it don't do that to run your programme follow following steps
1.open command prompt
2. go to the path where you have your python programme
3. use command for python for eg: python programme.py in linux
4. if it says python command not recognised probably because the path to python executable is not present in system path add it to environment variable and that should work fine

Categories

Resources