.py file opens PyCharm instead of running the script - python

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

Related

How can I change a PowerShell terminal to a simple one in VScode for Python?

I am new to VScode and want to run Python in it for my college project. I have seen that in VScode all the programmes are executed in Windows PowerShell Terminal(By default). But the problem is that it also shows the file address which is being executed which I don't want. So, please could you give a suggestion which software should be used in the terminal which only executes code and doesn't show any file address. And How can I change it?
VS code supports multiple shells. By default it's powershell (PS >) on windows
Click on "Select Default Profile" to change it (And restart VS code) or you can directly click on any of the available shells (shown in below pic).
About hiding the "file address" - you can always change the shell's corresponding settings file which it reads on load.
Ex: bash shell uses .bashrc settings file. And how to hide file path could be checked here for bash : Show only current directory name (not full path) on bash prompt
Similarly check how it's done for the shell you want
You can try the extension of Code Runner, it will be like this:
Otherwise, you can change the value of "console" in the launch.json file to the internalConsole, then run the python file in the debug mode(F5), it will be like this:
ctrl+shift+ is a Command Used to open terminal in VS code .. You can also Try Extension of python shell or powershell in VSCode ...

How to enable the "idle" command from Windows Command Prompt

I remember that at some point I used to be able open up the python IDLE from the command line by simply typing "idle" return, which would pop up the python idle editor. I recently got a new machine and installed python, but have to open idle from the windows start menu.
When trying to run "idle" or "idle3" from the command line I get
C:\Users\SSims>idle3
'idle3' is not recognized as an internal or external command,
operable program or batch file.
Can anyone tell me how to get the command line "idle" command set up. Thanks for any help!
There are no step-by-step fixes that I have found online to answer this question, but many that answer specific portions of the question scattered around online. Hopefully, this will help others with the same question in the future, as I know there are many who could benefit from this answer.
If you have selected "Add Python X.x to Path Variables" upon installation then you should be able to run "python" from the command line to start the python editor. Similarly, this will allow you to do the same for IDLE.
First, hit your windows key and type "path" then enter to go to your system environment page in settings.
Next, click "Environment Variables then select "Path" under user variables
There should be a popup that contains a path that looks like this:
C:\Users\SSims\AppData\Local\Programs\Python\Python37-32\
Copy that address then click "new"
Paste that address, then at the end of it type "Lib\idlelib" so that your new variable now looks like:
C:\Users\SSims\AppData\Local\Programs\Python\Python37-32\Lib\idlelib
This should be the path to the folder in which the idle executable file resides.
Note: You can check that path to ensure it is right by pasting it into the windows start menu and it should redirect you to the folder. If it doesn't, you can paste the path to the python folder from earlier that was added automatically upon installation and search around in there until you find the idle executable, then add that folder to the Path variables to get it to work.
You should now be able to run the idle command from command prompt, hope this helps!!
Instead of IDLE use, python -m idlelib command
check the IDLE official document

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.

can't execute pyw on windows

I want to hide the console window of a python program, so I change the file extensions to "pyw", but when I open it, the python IDLE show up even though I choose open it with "pythonw.exe"
If I use "pythonw test.py" in cmd, it works.
So I want to know what's wrong with this and how to solve this, thank you.
Change the program that opens python files.
Assuming you're using Windows, right click any python file (in your case any .pyw file, not .py), properties, change Opens with to pythonw instead of IDLE
For me, I had multiple version of Python installed that was causing issues. Once I had only had one version, I applied that pythonw.exe was the default for .pyw files and it worked.

Keep Windows Console open after a Python Error

File associations on my machine (winxp home) are such that a python script is directly opened with the python interpreter. If I double click on a python script a console window runs and every thing is fine - as long as there is no syntax error in the script.
In that case the console window opens up for a moment but it is closed immediately. Too fast to read the error message.
Of course their would be the possibility to manually open a console window and to execute the script by typing python myscript.py but I am sure that there is a more convenient (i.e. "double click based") solution.
Make a batch file:
C:\Python26\python.exe %1
IF %ERRORLEVEL% NEQ 0 PAUSE
Use that as your file association instead of python.exe directly. This will only cause the PAUSE statement to execute if python.exe returns an error
The canonical way to run a command in a command prompt window that doesn't close is
cmd /k "your command"
The way that I do it is i right click the script that I saved from notepad to a .py file, then i click edit with IDLE, This is an editing thingy, but you can also run modules from it
I've left a comment at rossipedia's answer about a similar situation, but I doubt it's gonna get noticed in a 8 year old post. So, I've experimented myself.
I have a python script that processes a folder that's dropped on it. As I add new lines, sometimes I get syntax errors, and I can't view them because the command prompt window closes too quickly. In order to keep the window open after an error, I had to modify rossipedia's code. I needed a way to pass a path to the bat file (drop a folder on it) and make the bat pass that path to the python script. The following does that:
debug.bat:
#echo off
REM debug.bat and myscript.py should be in the same dir, otherwise use absolute path for the python script
C:\Users\%username%\AppData\Local\Programs\Python\Python36-32\python.exe "%~dp0\myscript.py" %1
IF %ERRORLEVEL% NEQ 0 PAUSE
myscript.py:
import sys
print("printing dropped file path:")
print(sys.argv[1])
input()
If I get an error, I just use the debug.bat to view the error, and it works the same (drop a folder on it).
Update:
In my actual python script, I have this line:
sys.path.append("modules")
This doesn't work when I use debug.bat. "modules" is a folder that's in the same folder with the python script. When the script is called with debug.bat, the current working directory changes to the dropped file's directory. So, to get the path to "modules", I had to use absolute path:
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "\\modules")

Categories

Resources