How to fix run issues in vscode for python scripts - python

I have been using vscode for only css and html and pycharm for python and lately I have switched to vscode also for my python scripts . I did all the necessary configurations and extensions installations I even installed code runner but so far I have not been able to properly run my python scripts without passing through the debug face when I click on the run button it still shows only the output from the previous run.

I believe you are trying to run the code like a Jupyter notebook. If you highlight code and press shift+enter it should turn on a python interpreter that runs the code in a new pane.

Related

VS code is not running python

I am veryy new in coding and wanted to get into it. So I downloaded VS code. But I tried to run a very simple python command and it does not let me. See the picture below.
Visual Studio code is not an IDE, it is a code editor, which means you cannot run nor debug code naturally, for that you would need to run it on the windows terminal, or install extra packages and dependencies. If youre new and just want to learn Python, i suggest starting with a python IDE like the standard one provided or something more advanced like Pycharm, and if you want to program on VsCode and in multiple languages, search some tutorials about how to compile and run code on the windows terminal.
Provide more details about how you installed python from Microsoft Store, from anaconda or via its official page https://www.python.org/downloads/. Try running
python --version
or
python3 --version
If so you can run a python file from terminal. Assuming my file is called test.py
python test.py

VSCode not selecting the correct python interpreter

I have a script that I need to run using a very specific python interpreter in my hard drive.
I am using vscode as the IDE and trying to select the python interpreter using the command palette.
However, since the concerned interpreter is not present, I manually search it.
However, even after that, it doesn't work as typing python in the terminal then opens the windows store for installing a python IDE. I also tried adding the interpreter's path to the environment variables but the problem still persists.
How do I solve this issue.
Thank You for the help.
The Python version you selected on VS Code only effects the editor. Right click to any file on the sidebar and select Open in Integrated Terminal. If your selected Python version is created with virtualenv it will activated and your python command works.
If not worked and you want a global Python version, you need to add Python path to your system variables to work python command. Check this:
https://geek-university.com/python/add-python-to-the-windows-path/
Btw I don't think your python.exe in FreeCad/bin directory is proper Python version
At the bottom left you can select the Python interpreter.
Here it shows you on -how to do that: Python Interpreter
Reason:
Your system environment path contains this: %USERPROFILE%\AppData\Local\Microsoft\WindowsApps.
Solution:
Open Window Search(Win) to find Manage app execution aliases. To turn off App Installer of python3 like this:
You can refer to this answer for more information.

Visual studio code is stuck on Select Python Interpreter

I am trying to run a jupyter notebook on my Windows machine by using WSL2 to run it in Ubuntu 20.04. It was working perfectly fine yesterday but today it's not selecting a Python Interpreter. I have tried reinstalling Ubuntu, Anaconda and Visual Studio Code.
When I run
code path_to_notebook.ipynb
It opens the window below:
visual_studio_code
After a couple seconds the box (that I've made a yellow square around) pops up. When I click in the "Select Python Interpreter" button and select the interpreter that I want (~/anaconda3/bin/python) nothing happens.
The Select Python Interpreter (red box) also stays there.
What should I do?
I've gotten around the problem by using $ jupyter notebook --no-browser in the terminal.
Unfortunately, this means that additional computer resources are required to run the notebook in my browser over a text editor. But it gets the job done.

ModuleNotFoundError: No module named 'requests' (VS Code with Anaconda interpreter)

Hi I'm really new on the programming. I am using VS Code with Anaconda interpreter:
Here
I am using Windows 10
I want to import "requests" module on my VS Code. I opened cmd and wrote "pip3 install requests" but it told me "Requirment already satisfied:" etc... Then I tried to download with Anaconda. I opened my cmd and wrote "conda install requests" and I downloaded.
Then I wrote this code to try module: Here
But I received an error in VS Code like that: Here
What can I do to solve this problem? Thank you so much for your answers. (By the way, sorry for my bad English :D I hope I explained my problem.)
Click in bottom left cornet in VS code on (Python <Version>) and then choose the python version that in your system.
I have just found the answer. In VS Code, I open powershell and write "py (filename).py". But just now, I tried to click top-right button which name is "Run Python File in Terminal" (green button). Then code executed successfully. I didn't take an error.
Check the following prospects in vscode with python extension:
When selecting interpreter, press ctrl+shift+' to open a new integrated terminal. If you selected base:conda environment, the terminal should be like this:
Then use pip list to show modules you've installed, if module requests not there, type commands pip install requestsin the terminal;
click then create a launch.json file, select Python File. This creates and opens a launch.json. You can close it or add configurations you want. More guidance about debug configurations you can reference: Python debug configurations in Visual Studio Code. Press F5 to run your code.
Just make sure the module installation is in the same environment as interpreter you've selected.

Error running python program from Teminal but it runs fine in Pycharm

I am using python2.7 in Pycharm and program runs fine when started from Pycharm
the output from Pycharm is
I am running the same program form terminal it shows error as shown below
After some discussions, I did the following
I changed the project interpreter in python to which python2 path as shown below
Then running program from Pycharm works
but running it from terminal still have the same issue
the reason it was not working is that I was trying to import the XenAPI which is located in some other location as shown below, which pycharm is able to access by using import vkey.XenAPI but from terminal its not.
Now this problem can be solved in 2 ways
By using virtualenv , follow this tutorial
The good news is that we can directly install the XenAPI package using pip as shown below
Here is the screenshot which shows its working

Categories