python `os.system("xdg-open test.pdf")` doesn't open file - python

I'm trying to run the xdg-open test.pdf command from within a python script, but nothing happens when I execute os.system("xdg-open test.pdf") even though it outputs code 0, and even though after executing os.system("ls") it does show the file test.pdf. I've also tried subprocess.run("xdg-open test.pdf", shell=True) and subprocess.call(["xdg-open", "test.pdf"]). On the other hand, if I run xdg-open test.pdf from the terminal (I'm on Ubuntu), it does open the pdf. What explains this problem and how can I fix it?

Related

How to run a Python file from a Batch File with Anaconda prompt on Windows 10?

I created a script in python and I need it to run in way that the python code is not visible. I tried different methods like implementing nodeJS and PHP, but to no avail. So, I thought of using a .bat file to run the python file. I created a bat file for the python script, but on clicking the batch file all I got was an error saying that a module was missing in the code. The code works perfectly when I run it in Spyder and on the Anaconda Prompt. So, I thought of running the file from a batch file via the Anaconda Prompt. The code in the batch file is below:
%windir%\System32\cmd.exe /K ""C:\Program Files\Anaconda3\Scripts\activate.bat" "C:\Program Files\Anaconda3""
python myscript.py
pause
The .bat file now opens a command prompt in the python environment, but does not run the python file. I tried altering the code, but it was always the same result. If I type:
python myscript.py
into the next line in the command prompt, the python file gets executed. I need to find a way for the file to be executed without typing anything in the prompt. The python file contains GUI from tkinter and the shebang line has the python version (3.7.4). I work with Windows 10.
I tried this code in the batch file as well,
"C:\Program Files\Anaconda3\python.exe" "C:\Users\Sathya\Desktop\myscript.py"
pause
All this did was open and close the command prompt in the blink of an eye. I tried to run the bat file a few more times, but got the same result. But, this code worked on a different file (also with tkinter GUI) myscript_test.py and this one gave the GUI window correctly. I don't have a clue as to why it worked on the second one and not the first one.

In VS Code, can one run Python code in an integrated Python terminal like in Spyder?

Currently, in Visual Studio Code (under Windows 10 64bits), at a Python file called path\myfile.py, if one clicks with mouse right-button for context menu and then chooses 'Run Python File in Terminal', an integrated CMD terminal is open and file is automatically run there with:
python.exe path\myfile.py
After the file stops running, one is naturally left at the integrated CMD cursor.
This behavior is quite different, for instance, from what one has with an IDE like Spyder. There, when you run code (e.g. with F5), at the end one is left still at the Python cursor and can access content of variables created when code was run.
Is there a way to achieve a similar behavior in Visual Studio Code?
You can configure VS Code Python extension to use the -i command line option
Described in https://docs.python.org/3/using/cmdline.html#cmdoption-i
You only have to add the setting bellow (inside settings.json file)
"python.terminal.launchArgs": ["-i"],
This will execute the command python.exe -i path\myfile.py.
I don't know if it is a new feature, but I've been using it for while.
If you would like to use the terminal IPython, like in Spyder, you can use a different set of options, as the following:
"python.terminal.launchArgs": ["-m","IPython","-i"],
With these, VS Code will execute the command python.exe -m IPython -i path\myfile.py.
Then, it will run IPython module as a "script" (with -m option), which will use the options -i path\myfile.py, i.e., IPython will run the file and remain opened.
BTW, another thing is: you can run "cells" in Spyder's integrarted terminal (regions of code with #%%). But in VS Code it seems you can't.
I've made a question with a "work around" to run cells of Python files in VS Code Integrated terminal, which is posted Here
Yes. Open a terminal window and it's like a terminal window on your computer. You can type python filepathandname and the python script will execute like it does from the command line.
The closest you can come is to run the code under the debugger and set a breakpoint at the end to pause the exiting of the execution. Otherwise feel free to file a feature request at https://github.com/microsoft/vscode-python.

.py file opens PyCharm instead of running the script

I have written a python script (lets call it myTest.py) and would like to launch it from the windows command promt.
My python interpreter was added to the PATH environment variable, therefore i can launch it just fine as python myTest.py and it is executed successfully.
I would however like to launch it by simply typing myTest.py in the command promt. But if I do this, the file is opened in PyCharm and is not executed.
I have checked assoc .py which gives me .py=Python.File, as well as ftype Python.File which gives me Python.File="C:\WINDOWS\py.exe" "%L" %*. As far as I understand this, a .py file should be launched with py.exe at the given path.
(If you are as confused as I was by the %L option of ftype, that is not mentioned in its help document: It makes no difference in my case and is equivallent to %0 and %1. Source: https://mail.python.org/pipermail/python-list/2015-November/699263.html)
Can anyone explain why my script is opened in PyCharm and not launched with py.exe? What do I have to do to simply execute it instead?
Edit:
The answer of #Hamed169 solves the problem (thanks!), but doesnt't give any details on why there is a difference between the program that is used to open a file and the one that is returned by ftype.
For everyone that is interested in why there is a difference I have found the following question (has currently no final answer, but quite a bit of information): https://superuser.com/questions/204354/how-do-i-get-ftype-assoc-to-match-windows-explorer
Right Click on a .py file
Click Properties
Click "change" in Opens with:
Select your python interpreter instead PyCharm
Run your .py files and enjoy:)
Thanks a lot Hame
We should change it to the python.exe file path, i.e., (in my case):
C:\Users\NEDA\AppData\Local\Programs\Python\Python38-32

A python 3.5 script not printing results in windows command shell or powershell, only in the interpreter

I'm trying out some data science tutorials with python and can't get print to, well, print! when I run a .py in windows command shell or powershell. Print does work when I use the interpreter, but I have to type it in line by line (I'm not seeing how to run a .py as a file in the interpreter). I'm attaching snips of the file, and a snip of me running in the interpreter. I tried to attach snips of what happens when I run in command shell and powershell, but apparently I need at least 10 reputation points before I can post more than 2 links. Admittedly, those snips weren't interesting; there is no error and nothing printed. It just runs and returns to the prompt.
Also, as a test, I saved a .py file that simply does print ("Hello") and it does print correctly in windows command prompt and powershell.
Thanks in advance for any help!
Casie
PY script
Snip From Python Shell
Is that image from the IDLE console? To run the script you are editing, use menu Run > Run Module, or F5. Every python GUIs has an equivalent feature.
To run an arbitrary script from inside the commandline interpreter, say mywork.py: As long as it's in the working directory that you were in when you started the interpreter, you run it (once) by typing import mywork. (As you can see, its name must be a python identifier.)
Edit: You'd get to the bottom of this a lot quicker if you'd put print("Hello, world") in a script by itself and run it. From over here it looks like it would print just fine, proving there's nothing wrong with your python interpreter.
Your script has a bug, though: As soon as you enter the function random_kid(), you leave it again since you return a value on the first line. All those print statements are never executed. Why you think it works differently with %run I can't say, but for sure this function cannot print any output.

The output of a python script

So I am trying to debug some Python code that I am running, because for some reason when I run the code in a command line it works perfectly, but when I make it a .py file and run it that way it fails.
The output is in a Windows Commander window, and it disappears before I can read it.
Is there a way to see what this output is?
Open a windows commander, and then go to the dir where a.py is, run "python a.py", it will not disappear!

Categories

Resources