Running Python scripts simultaneously in VS Code - python

Consider the following scenario: I have two Python scripts, the first a long-running process and the second a short-running process. I heavily use the shift + enter shortcut to run code directly from my .py files.
Currently in VS Code, I can only manage to have the interactive environment with one script. That is, if I run the first, long-running script and then open a new Python terminal window, running a line such as print('hello world') will run in the first terminal. I want to learn how to change the shift + enter so that it executes in the second, newly-opened terminal window.
I've looked at a few SO questions, namely this one, but the solutions either don't work or are not applicable to my use case.

I managed to execute in the newly-opened terminal window typing a command there. Not as convenient as shift + enter but does the job.

Related

Python Input function in atom prevents code from running [duplicate]

I created a script which just asks a user for their name and age using Python's input() function.
I installed the package Script. This ran the script well but couldn’t deal with the input.
I have also tried a number of other options but haven’t had any success.
Any ideas how to build and execute scripts from within Atom? I don’t mind if it just simply saved the script and opened Pythons IDLE at a minimum.
Add Terminal-Plus and run the code with the python name_file.py command
Script Runner can run scripts and supports input, unlike Script. It's the simplest full terminal package that I know of. To run a script, press Alt+X
For more advanced usage, you might look at Hydrogen.
The atom-python-run package gets around the input("") freeze problem by opening a terminal window and running the code in that.
Doing it within Atom has eluded me too, but this works OK.

save Python variables after running it from terminal

I am using python and Visual Studio code to develop my code. I can run my scripts successfully (Using Run Python File in Terminal). However, this is very inconvenient since after each run I go back to the terminal window and do not have access to python, therefore, I will lose the variables in the previous run so I have three options: 1- Print all the variables I want to screen (This is clearly not scalable). 2- Run the file again. 3- Run the Python file in an interactive window(Ipython).
Option 3 is exactly like running the file in a Jupyter Notebook which is very convenient since you can create a cell below the current cell and keep using the variables after one run.
I would like to know what is the best method to run the python script from the terminal (Pycharm, VS code,...) and can work with variables after (not like every time you forgot to print something you run the file again?
If you set a breakpoint then debug your code (f5), on the bottom of VSCode there is a Debug Console, you can type in debug command there (print(), type(), etc). That seems like it might be what you are looking for.
Try shift + enter on each line this will open the terminal with python
Or try to run .ipynb files instead of .py files in vscode

python 3.6.2 not giving me option to create or run files or anything of the sort

I know some basics of Java and C++, and am looking to learn Python
I am trying to develop some random stuffs to get a good feel of how it works, but i can only make 1 line scripts that run every time i press enter to go to the next line.
I've seen tutorial videos where they can just open up files from a menu and type away until they eventually run the program.
I'm using IDLE, and i don't see options to open up new stuffs; I can only make one or two line programs. When i tried to make a calculator program, i didnt know how to run it because it ran every line of code i typed in unless there were ...'s under the >>>'s.
I think it's because i am in interactive mode, whatever that is.
How do i turn it off, if that's the problem?
There are many different options for writing python scripts. The simplest to use is Idle, it come with the Python download. Within Idle, create a new document to write a script. Once finished, save it as a .py file, and you can run it within Idle. For my personal setup, I use the text editor Atom. I can create documents easily, and run them through the terminal on my computer. Hope this helps.
To exit out of the "interactive mode" that you mentioned (the included REPL shell in IDLE) and write a script, you will have to create a new file by either selecting the option from the top navigation bar or pressing Control-N. As for running the file, there's also that option on the navigation bar; alternatively, you can press F5 to run the program.

Making python wait before running an external program for the second time without a for loop

I am using VisualDoc to iterate the material properties in a finite element software (MSC MARC).
The problem I am having is that VisualDoc executes multiple windows of the FEM simulation at once and I need the FEM program to execute one run at a time.
In visual doc the MARC program is called as follows: a cmd batch file is called which opens a python script which opens the MARC simulation. (I tried multiple ways of calling MARC from VisualDOC and this was the only way that worked well)
First I tried to set the MARC software to non-batch mode, but I cannot find the settings to do that.
At the moment I'm trying to write a python script that uses
subprocess.Popen and it's accompanying wait() function, but I manually asked Python to run again while the first MARC run was still executing and it still opened a second MARC simulation. Is there a way to force Python to wait for the program to finish executing before it opens the next run?
p = subprocess.Popen(["procedure.proc"], shell = True)
p.wait()
I thought of trying to use cmd for the same effect, but since python opens the MARC program I think it is most probably better to code the wait into python. Am I right in this assumption or would it be better to enforce the wait through cmd?
The current cmd code is as follows:
CD C:\INTERFACEMARC\loop
openmarc.py
Note:
I am unable to use a for loop as the python script should open the MARC program only once in order to work with my VisualDoc optimization code.
At the moment the code for opening MARC looks as follows, but as far as I understand this does not prevent the python script from executing a second time while MARC is still running (based on my manual test).

Opening terminal using python

Currently I've been creating just a small program. And one of the options is to start a counter.
Code for counter
from time import sleep
number = 0
while 1 == 1:
sleep(1)
print number
number += 1
I want to be able to open this program in a new terminal window (Ubuntu) so I can keep the existing terminal open. Anyway to do this in python?
Thanks
Paul
EDIT
I think I figured out one way to run it in the same terminal so it works!
os.system('python counter.py')
I simply used that, I thought if I were to do that before, when I used CTRL + C it would close down both programs but this seems to of worked. Thanks for the answers!
You could create a wrapper that launches your terminal and tells it to run your script. If you know which terminal you're using, this isn't too hard. For example, if you use multi-gnome-terminal:
#!/bin/sh
multi-gnome-terminal --use-factory --command /usr/bin/python /path/to/my/script.py
Now, every time you run that wrapper (with sh ./wrapper, or /usr/local/bin/wrapper if you install -m755 it, or by double-clicking it, or whatever), it will open a new window (launching a new terminal process only if necessary) and run your script there.

Categories