Launching python3 script using a python2 script - python

is there a way to launch a script running on python3 via a python2 script.
To explain briefly I need to start the python3 script when starting the python2 script.
Python3 script is a video stream server (using Flask) and have to run simultaneously from the python2 script (not python3 script first and then python2 script).
The ideal would be to get a function in the python2 script which "open a cmd window and write" it in : python3 script_python3.py

create command in bash file. command is to run script_python3.py, ex: python script_python3.py
then from python2 file, run the .bash file. ex:
import os
os.popen('sh /scripts/my_bash.sh')

You could use something like explained in this answer.
That use case of that question is a bit different, but the answer should work.

Related

How could I use python to open gnome-terminal and then run python commands in a multiline manner?

I am attempting to get a subprocess call that will open a gnome-terminal and in that terminal enter python then execute some python commands and imports without the user needing to type them out.
I'm working on on some automated terminal opening code that will open a gnome-terminal window using subprocess.call (Open new gnome-terminal and run command) also (Python syntax to open gnome-terminal and execute multiple commands)
My end goal is to open up a gnome-terminal window and with the same script that opened the gnome-terminal, enter the command to use python. And then in python import a package and run it.
My current usage is:
subprocess.call(['gnome-terminal', '-e', "python client.py"])
However what Im trying to get to is an importable package that I can open several gnome terminal windows for that will call different objects from a pypi package effectively doing the same thing that call client.py would do with the files. This doesnt work with packages installed in pip however.
What I want to do is something along the lines of:
subprocess.call(['gnome-terminal', '-e', "python && import <package> && c = <package>.obj.func()"])
So that a terminal would open and enter python, import the package I want, then call something from it, but all as instructed by a python file
This doesnt appear to work as multiline scripting works for stuff like bash scripting but doesnt seem to work when trying to enter commands after python has been entered.
Any advice would be greatly appreciated
I don't have Gnome Terminal installed, but if you can get that to start Python correctly, then you can use Python's -i flag to run a set of commands or a script.
The two usages are as follows:
python -i path/to/my/script run the script then enter the interpreter
python -i -c "# Some Python commands" run the command(s) then enter the interpreter
For example:
$ python -i -c "import this"
[poetry]
>>>
# Ready for input!

Running a python script via Powershell script

I have a python script which I can run via PowerShell using the following code:
cd User\PythonScripts
python TestFile.py
Now I want to run these simple commands via a PowerShell script (notepad file and saving it as a ps1 file).
I have googled a lot but I cannot find an answer, but I think it should be something like this:
$path = 'C:\User\PythonScripts'
$file = 'TestFile.py
I think I still miss the reference to python (so it knows which program he needs). How do I need to do this?
Assuming that python is already in your path variables you can just call a python script like this:
python C:\User\PythonScripts\TestFile.py
I think the question is you want to run the python script using powershell .
I think below code will do for you
$path = 'C:\User\PythonScripts'
$file = 'TestFile.py'
$cmd = $path+"\\"+$file # This line of code will create the concatenate the path and file
Start-Process $cmd # This line will execute the cmd
Save the above code as .ps1 and run the that powershell file
I have been facing the same problem with Windows10 PowerShell and I have solved it like this:
Go to PowerShell and instead of running the command like this:
PS C:\Windows\system32> python ex1.py
run it as below:
PS C:\Windows\system32> python C:\Users\PCWIZARD\Desktop\hardway\ex1.py
In other words specify the entire path of your ex1.py depending on where you saved your file on your computer. This will print out your code.
I think below code will work.
cd C:\User\PythonScripts
.\python TestFile.py
Mention python.exe if it is a executable in place of python.
Save above code in .ps1 file and run it on powershell

What is the equivalent of this Linux command on Windows cmd?

This command sets an environment variable ("CUDA_VISIBLE_DEVICES") for a python script before running.
$ CUDA_VISIBLE_DEVICES=2,3 python my_script.py # Uses GPUs 2 and 3.
It works fine on a Linux machine but on Windows, it says that
'CUDA_VISIBLE_DEVICES' is not recognized as an internal or external command
Is it possible to do that on Windows without altering the python script?
For a one liner simply run on Windows:
set CUDA_VISIBLE_DEVICES=2,3 & python my_script.py
For windows, this worked for me too, thanks to Adonis. you have two options
on the command line you can use the following command
set TF_CUDNN_USE_AUTOTUNE=0 CUDA_VISIBLE_DEVICES=1 & python demo\singleperson.py
on the main python file, put the following lines, below import things
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="1"
then run the python file on the command line
python demo\singleperson.py

Launching a python script in a new terminal from another python script

I'm using LXDE, I would like to Launch a python script in a new terminal from another python script.
I would like the new python script to be totally independent.
I have tried a lot of things...
Calling xterm (or x-terminal-emulator) directly from python with the subprocess.call or subprocess.Popen with or without shell=True argument, it didn't work. It gives me an error about display not being set.
I have also created a sh file which calls the other python script and tried to call it, same results.
Any way to do this?
This works fine for me:
blocking:
import os
os.system("xterm -e \"python christmaskittens.py\"")
non blocking:
import os
os.system("xterm -e \"python christmaskittens.py\" &")

What is the meaning of command line options for python from the windows command prompt (example: -i, -m)

When running python scripts from the command line, sometimes one has to use options like:
script.py
python script.py
python -m script.py
python -i script.py
What do these options do? Where can I find a full list of these options?
I'm asking because I wrote a gui using tkinter, and when I run it using either of the first two methods above I get an ImportError for tkinter, but the -i option works fine. Can I write a python script that defaults to some option (-i) when it is run?
EDIT: Here is the traceback regarding my ImporError:
it gives the file name and then:
import tkinter
ImportError: No module named tkinter
I am using python 3.5.2, and this error doesn't appear if I use
python -i script.py
You can get a list of all the python command flags and what they do by typing
python --help
In the case of tkinter, you need to run with the -i flag because it will run in "interactive mode". This allows the windowed application to launch. The help says it best
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
Meanwhile -m launches a module as if it were a script.
-m mod : run library module as a script (terminates option list)

Categories