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

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

Related

Launching python3 script using a python2 script

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.

How to run a script in Idle shell when double-clicked in Window OS?

Summary:
Windows User, that has python installed on his/her machine, double-clicks a (.py) or (.bat) file and it launches myapp.py in the Python Idle Shell.
Alternatively, the myapp.py is able to launch itself in Python Idle Shell after getting double clicked but that is probably not feasible.
More rambly (to prevent misunderstanding):
If someone wants to run a (.py) file via Python Idle Shell, they would right-click on the file > 'Edit with IDLE' > 'Run Module (F5)'. How can I run the (.py) file via Python Idle Shell with just double-clicking a (.py) file or (.bat) file?
Additional info
OS: Windows 7 (x64)
Python: 2.7
As I said in a comment, from the Command line usage section of the Python 2 Idle documentation it sounded like you could do it in a batch file by starting idle and passing it the -r file option to get it to run your script. (Note for Python 3, here's the Command line usage section.)
Today I had a chance to explore this further myself and can report that it does indeed appear to work. So now here's what I meant in more concrete terms.
The following batch file is what I used on my Windows 7 (x64) system with the 32-bit version Python 2.7.14 installed. You will likely have to tweak the file paths used in it to suit your own configuration.
idletest.bat
#echo on
python "C:\Python\Lib\idlelib\idle.py" -r "idletest.py"
pause
If Python is in your windows path, one way to interpret / “execute” myapp.py in Python via a batch script (.bat) could be :
Starter.bat :
#echo off
title “MyApp Launcher”
python2 myapp.py
pause
exit
However it does not specifically use the IDLE application !
Also the .bat must be in the same folder (or the path between both elements should be fixed) that myapp.py .
NB : Depending on your installation of Python, if it do not work, try replacing python2 by python or by py

How to execute Python scripts with python command

I want to run the Python Script by using python command from IDLE Python GUI.
The location of the file is C:\Users\DELL\Desktop\Hello.py
When I run python C:\Users\DELL\Desktop\Hello.py with Windows command promt, it works. However, when I try with IDLE Python GUI and Python (command line), it does not work and gives me a message SyntaxError: invalid syntax
Capture
inside python shell, you can import the module. This way, you can see "Hello" printed on shell once u import it.
>>> import sys
>>> sys.path.append('C:\Users\DELL\Desktop')
>>> import Hello
"Hello"
When using IDLE, you are already "inside" python (in its REPL)..
You should "load" (import) the file instead..
See here https://stackoverflow.com/a/21650698/5121955 (exact same situation), where you can find many solutions with their advantages..
You cannot do this from the Python interpreter, since this is not Python syntax, if you want to do this it has to be from command prompt or execute it as a system command:
import os
os.system('python C:\\Users\\DELL\Desktop\\Hello.py')
Or use subprocess.call
If you want to run your python file from another Python file see How can I make one python file run another?
If you want to run it from the IDLE simply select open and navigate to the desired location and select run.
If it is executable script (as you stated, it works from command line with the python command), you should be able to execute it directly from python with the following statements
import os
os.system("C:\\Users\\DELL\\Desktop\\Hello.py")

Running Python script from cmd line but start with import in code

I am running a Python script from Linux command line, and the script itself, on the first line, import several modules. I got some error message and searched online. Here is a reply from the author of the Python script:
it appears that you are running dexseq_count.py as if it were a shell script, rather than from Python. As a consequence, the first line of the script is interpreted as the Linux command 'import' rather than as Python code, leading to the error you report.
I am curious if the first line of import in Python has been mis-interpretated in Linux, and if so, how can I solve this problem? I have to run in the cmd line instead of in Python.
Thanks so much!
Two solutions here:
You can run the script using python like this: python my_program.py or add this at the top of the file: #!/usr/bin/env python which will switch from bash to python to run this script

Run python source file from PowerShell

I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.
I use 'python myfile.py' when I try to execute the script.
If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?
Here is myfile.py. I'm running python 2.7
# filename: myfile.py
while True:
s = raw_input('Enter something: ')
if s == 'Quit':
break
print 'Lenght of the string is', len(s)
print 'Done'
You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.
If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py
I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.
Edit after Update:
Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.
You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.
Disclaimer: I don't know PowerShell, but I do know cmd.exe.
I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.
If you started by typing "python" in powershell you will need to get out of that script.
If you are in python type:
quit()
then type
python myfile.py
This should work if your python is installed correctly.
Try to type this in Powershell:
$env:path="$env:Path;C:\Python33
After this, command
python yourfile.py
should work.
This my sound silly, especially coming from a beginner.
Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works

Categories