PyCharm debug console is read only, no command prompt - python

I am using PyCharm 2021.2.4 on macOS Big Sur 11.6 with my interpreter running Python 3.9
My PyCharm debugger console displays as read only without the command prompt. I need the prompt so I can set the proper variables when I launch the script.
Here's what my debugger console looks like:
Here's the prompt I'm expecting as per the Jet Brains documentation: https://www.jetbrains.com/help/pycharm/using-debug-console.html#open
So far, I have tried:
Verifying that "Emulate terminal in console output" is deactivated as per: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000244824-Console-toolbar-is-gone?page=1#community_comment_115000347810
Installing PyCharm 2020.3 to check whether an earlier version exhibits the same behavior (it does).
Creating a conda environment using Python 2.7 as the interpreter.
Resetting the IDE to its default settings.
And in case it's relevant, I'm trying to run construal_level_task.py which is the file in this repo https://github.com/UOSAN/high_level_construal
At the command line, the file is launched with this command: python3 construal_level_task.py --id ASH999 --run 1 --session 1 and those inputs (id, run, session) are the ones I'm trying to enter in the debugger console to run the script in debug mode.

The python program exited, so you cannot interact with the console any more. You need to set a break point in your code. Then the debug console will become interactive.
those inputs (id, run, session) are the ones I'm trying to enter in the debugger console
Using the debug console is not the right way to enter inputs from command line arguments. Instead, you should edit the run configuration and add them there. See Pycharm and sys.argv arguments for an explanation of how to do this.

I think the problem is that you use conda interpreter. Is there a need for this? You can try using pure interpretator.
EDIT: Try to use virtual using normal interpreter (not conda). Your mistake is conflicting copies of conda envs.

Related

can I run a shell task in VSCode that uses a specific shell for a python virtualEnv?

I'm writing some python to render stuff that I tweak and run a lot, and that runs inside a virtual env. I would like a keyboard command to run a bash script (that launches python) inside the known terminal and virtual env.
I played a bit with setting up a shell script and a custom task, but entering the virtual env is always a bit tricky.
I don't need a debugger or anything complicated, just a way to run the python code and attach a keystroke to it.
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
https://code.visualstudio.com/docs/python/jupyter-support-py
You can just run the python.exe/python binary in the virtualenv folder as an executable.
So to run a specific python file with a virtual environment:
For Windows it would be .\path_to_virtualenv\Scripts\python.exe yourfile.py, and for Unix system it would be ./path_to_virtualenv/bin/python yourfile.py
And instead of running a python file, you can probably pipe the input command into the python executable in the path above if you want to run a specific python command.
So something like COMMAND | ./path_to_virtualenv/bin/python yourfile.py
Could you please explain the difference between the way you want and the following operation:
Use "Ctrl+Shift+P" and type "Python: Select Interpreter" (the same as click the interpreter in the lower right corner which is on the right side of python).
Use "Run Python".
I think there is no difference because before your using a keyboard command, you still need to choose the interpreter.
By the way, use jupyter notebook or interactive window will be a good chioce as well.

VS Code trying to run .../Activat.ps1 in cmd terminal

I'm using VS Code for a Python project using a virtualenv. I switched my deafult terminal from powershell to cmd as VS Code was not happy executing powershell scripts.
Now when I open a terminal in my project it opens cmd (as desired), but automatically tries tor run .../Scripts/Activate.ps1, which it doesn't like. I want it to run .../Scripts/Activate.bat as we are in cmd. Runnning it manually for now, but would be nice if I didn't have to.
No doubt there is a setting somewhere to change this, but I cannot find it. Any ideas?
This is a problem related to the Python extension, it should be fixed in the last update.
You can get some information from here.

VS Code execute selection to IPython shell in terminal (no notebook)

I wanted to use IPython interactive in VSC (not using a notebook). By using a keybiding for workbench.action.terminal.runSelectedText, I can successfully execute a selection to a python interactive shell, but with iPython it fails to run the cell and I have to change selection to the terminal and press enter.
Here's my keybidings.json file:
[
{
"key": "ctrl+enter",
"command": "workbench.action.terminal.runSelectedText"
}
]
Any help to solve this is much appreciated! Ideally, a keybinding configuration that includes an extra enter.
I have this problem when I upgraded IPython to version 7.30.1
The solution I found is that add "--simple-prompt", this is not perfect (just no color theme or auto completion), but at very least, you don't need to press enter when sending codes to run.
IPython is not officially supported by the Python extension and this is part of the reason: IPython's design simply requires you to press Enter an extra time based on how VS Code sends text into the terminal.
This is an issue with IPython's autoindent feature. You can disable this by passing --no-autoindent when launching IPython (i.e. ipython --no-autoindent).
edit: added gif.
I was annoyed by this quirk in vs-code too.
I noticed that when using a python virtual environment (I use pipenv) my selected line was executed in an Ipython terminal and no extra Enter key press was needed.
To reproduce or test out:
Launch VS Code
Open a python file and a terminal window
In the terminal run pipenv shell (requires pip install pipenv && pipenv install ipython)
Then in the terminal run ipython (or ipython --no-autoindent)
Select a line in your python file and from a vs-code command palette execute Terminal: Run Selected Text in Active Terminal (or use a keyboard shortcut)

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.

what can I do if a program can be run by python console while can not be run by ipython console?

It seems that the spyder has removed python console, but I got a program can only be run by python console, what can I do? or is there any thing I am wrong?
I got some codes from github, and it needs ADB driver for Android, after I installed ADB, I can run the program in cmd using python wechat_jump_auto.py, but cannot run in spyder with ipython.
In Spyder3 installed in Windows OS, we can add the path to adb using Tools --> Current user environment variables....
Here, we can add the path to adb.exe file by appending it to the path variable. Then, we need to restart Spyder3. Then you will be able to directly run your script with access to adb.exe from Spyder3 IPython console or simply by clicking Run button.
Just came across the same problem as you recently.
In fact, it seems that program using ADB tools just cannot run in Spyder even by python console (my Spyder IDE is equipped with both Ipython console and Python console).
One practical way to solve this problem is to run your code in cmd.
Open your cmd window and do something like this:
python "xxx(path)\xxxx.py(file name)"
In my case, it looks like this:
example image
Hit Enter, and hopefully your code will run successfully.
If it still cannot run, maybe you haven't set your environment variables correctly.
Hope this can solve your problem. Good luck :)

Categories

Resources