I have two folders, 1 and 2. Each folder has a Python file with the same name: 220_beta_1e-2_47_53ND.py. How do I run both the Python files simultaneously on different consoles? I mostly use Spyder.
on powershell (replace XYZ with your python version)
Invoke-Item C:\PythonXYZ\python.exe pathtoscript1; Invoke-Item C:\PythonXYZ\python.exe pathtoscript2;
or rather create a third script in python that launches 2 processes
Related
I have 2 python programs that act as 'services', each of them runs from its own venv. I'm looking to create a batch file to initiate both services, each on their own dedicated terminal.
I was able to create 1 batch file to initiate the venv and the service, but I cant merge them into one batch file.
CALL <PATH_TO_VENV_SCRIPTS>\activate
"<PATH_TO_PYTHON_FILE>main.py"
pause
When I run the file above, a terminal opens, activates the venv, and runs the main.py using the venv.
Now I'm trying to have 1 batch to start both services. I have the following:
START <PATH_TO_VENV1_ACTIVATE.BAT>
"<PATH_TO_SERVICE1_PYTHON_FILE>main.py"
START <PATH_TO_VENV2_ACTIVATE.BAT>
"<PATH_TO_SERVICE2_PYTHON_FILE>main.py"
The file above indeed opens 2 terminals, and activates both VENVs, but unfortunatelly the python files are initiated from a third terminal which opens before the aforementioned 2.
How can I get the python files to be initiated from their own VENV terminal?
Any help is greatly appreciated.
Thanks in advance!
You have to launch your python files as a command in your newly created venv terminal. If you start the python service from the parent script, they're executed in this terminal context.
In other words: You have to include the execution of the scripts into your PATH_TO_VENV1_ACTIVATE.BAT. However, if your python script does not terminate after execution, you won't ever get there and the "parent" terminal will hang on the first script. So make sure that the script runs in a way which does not block the parent calling context.
HTH
EDIT: If your scripts are starting a service anyway, consider creating a container and launch these from there. Therefore you have an encapsulated context paired with easy execution.
How can I execute three or more Python scripts in parallel?
Currently I am executing three Python scripts in three different consoles of Spyder.
One solution could be to import all the scripts and then simultaneously execute them in a function.
I can change the order to python2,3 folders in the system PATH variable. But what are other ways to do this?
There should be more elegant way to change versions of python i want to run.
e.g. in console:
python file.py #will run python2
and after i change python command to use python3, it should be the same:
python file.py #will use python3
I suppose you are trying to run your script with the correct interpreter depending on which python version was used. On Unix/Linux this is done with a so called “shebang” which is written in the very first line of the file. E.g.:
#!/usr/bin/env python3.6
If your installation of Python3 is newer than Python 3.3 you can use the python launcher for windows, which should be able to launch the correct version of Python depending on the shebang, even on window.
Also see here for more informations on shebangs.
If your concern is what Python version is executed when calling python in a console, then an alias or a stub script are the two ways to go.
This post will explain you how you can do this on Windows.
The alias way, just like it would be on a Unix system, is to create an alias, either temporary to the session or permanent, so that python now means C:\Python27\python, or whatever version you want.
The script approach consists in putting a script named python in a directory referred to in your PATH, and have that script run the right version of Python.
I highly doubt that this will affect all the batch scripts that call python, but it will definitely fire the right Python when you'll type python in a console.
Now, if you're concerned about what version a script is executed with, you can specify an explicit version with a shebang line, or manually select it by right-clicking the .py file and clicking open with.
I am trying to create a file sharing program using Python and to do that I am using a server. In order to test the program I need to use a different Python IDE to run the server and then use Sublime Text 3 to run the client. If possible, I would like to know if and how I can run both programs simultaneously in Sublime Text 3.
I feel it may just be best to run the python files via the terminal or command prompt. This would also in general be a better practice to run your programs. To run them, you would just need to type in... "python path/to/your/python/file"
Each window gets its own build process. You can open both files in separate windows, run the build on the server, then run the build on client and they will both run simultaneously.
Say Suppose, the name of my python script file is hello.
How the script can be executed ?
Sometimes, I see that most of the python scripts are executed by (python hello.py) and sometimes (./hello.py). Which one of these executing is true? If both are same, why it is mentioned as different commands?
The syntax ./hello.py is typically used on Unix-like systems (including Linux and OSX); it requires two things:
that hello.py has proper rights (execute bit set)
that the first line of hello.py is #!/usr/bin/python (or similar, depending on location of your Python interpreter)
The other form - python hello.py - does not have such requirements.