How can I create a workspace in Visual Studio Code? - python

I have this very beginner question that I happened to install Visual Studio Code on my Mac, and every time I tried to run a simple Python program on that, it said that I need a workspace to run, so how do I create the workspace?

I am not sure how you try to run this program, but you can just go to menu View → Terminal and type python your_program.py in TERMINAL from the folder where your program is located.
And please check if you have not accidentally installed Visual Studio instead of Visual Studio Code (those are two completely different programs).

VSCode workspaces are basically just folders. If you open an empty folder in VSCode it will get treated as a workspace, and you can add any scripts you want to it. VSCode will create a new hidden folder in the folder you chose that will hold settings for the workspace. For python, make sure you install the python extension (just grab the one with the most downloads) and follow the instructions there to make sure your python environment is properly configured. If you're using git, you might want to add that hidden folder to the gitignore.

Related

Why can't I run python in git bash and visual studio code?

I'm having a hard time understanding and running a terminal. I downloaded python 3.11 from the official website and installed it. I also checked the box at the beginning of the installment to add python to PATH. But when I try to run it on git bash it just gives me an empty line. On Visual code, It takes to download python through the Microsoft store. But is it makes sense to download it twice, through the website and through the store...?
git bash picture:
visual code terminal picture:
On Visual code, It takes to download python through the Microsoft store
If my understanding is correct, Windows comes with dummy python executables python.exe and python3.exe that take you to the microsoft store to install it- after which it is a real python executable instead of a dummy one. If you run where python, you'll get a list of all the python executables that are found via the PATH variable in the order that they are found in the PATH, where one of them will be the Windows one (instead of the one you installed from the Python website). Likely, the one you'll see listed first will be C:\Users\you\AppDate\Local\Microsoft\WindowsApps\python.exe.
What you probably need to do is update the order of entries in your PATH environment variable so that the directory containing the one you installed from the Python website is earlier in the PATH environment variable than the directory of the out-of-box Windows one.
Note: As indicated in this question: why must python executable path be added to system PATH environment variables for python command to work in visual studio code, you might need to add the directory containing your from-website Python to the system PATH environment variable (before/above the path to the directory of the system one) (as opposed to the user PATH environment variable).

how to change module path for python in visual studio code

I'm sorry if this is trivial, but I'm just starting python and I don't know a lot of terminology so bear with me.
background information:
I just recently changed my IDE from anaconda (Spyder) to Visual Studio Code and I realized that a lot of modules that I installed from pip and used to able to import on Spyder is now unavailable on Visual Studio Code. The only work around that I have been able to come across is to import sys and append the appropriate path to every single one of my project. This is obviously very annoying and is quite a bit of a hassle every time I try to start a new project. Since Spyder works fine for me, I'm convinced that the problems lies in the Visual Studio Code settings instead. But every articles and advices I've came across couldn't give me a straight answer without throwing terminology that I'm unfamiliar with left and right.
Request:
Can anyone help me to permanently append a new python module path to Visual Studio Code so that I don't have to import sys every time?
Thanks in advance!
You can use python interpreter path setting to point to a python installation/ venv
Set the path in .vscode/settings.json
{
"python.defaultInterpreterPath": "path to python.exe"
}
You can follow this for more info.
Python extension has a function called Python: Select Interpreter, which you can get from Command Palette.
If you didn't create virtual environment when using Anaconda, modules should be installed globally, for example python3.9.5.
If you installed modules in virtual environment, Python extension should also be able to detect and show them in the list by click the command Python: Select interpreter. If not, you can manually Enter interpreter path:
Choose the environment where locates your needed modules as the interpreter then you may import them successfully without extra import sys.
More information please refer to Using Python Environments in VS Code.

Breakpoints not getting hit in VSC even with justMyCode = false

I am developing in Python using Visual Studio Code. I put breakpoints in the code of an external package in my virtual environment. But the breakpoints are never getting hit. Even by setting the variable justMyCode = false in the VSC settings.
Could there be another reason ?
I encountered the same problem.
The reason is that "The venv folder is not used to store external packages. They must be activated to access the modules installed in them, but the code that uses these libraries should remain external."
The solution is to move the external files with breakpoints out of the virtual environment (like "venv") folder.

Eclipse opens compiled python instead of source code on breakpoint

I am having some trouble with my Eclipse debugger. Every time I set a breakpoint on Python source files, when the breakpoint is hit Eclipse opens up the associated pyc compiled file.
Below are a few things I have tried:
Creating a new Eclipse project with the code
Deleting all settings files within the code
Switch to a new workspace
Re-install eclipse and PyDev completely.
The problem persists. I don't know what could possibly be causing this. The issue only occurs when a breakpoint is set. However, that renders breakpoints useless in Eclipse.
This issue only began after I tried to set up Visual Studio for Python development. In Visual Studio this issue does not occur.
Can you check if you have pydevd installed somewhere in your system and remove it if that's the case? (i.e.: run without debugging import pydevd and if you find it remove it from your interpreter).
The PyDev debugger is used in other products (such as PyCharm and Visual Studio), so, my guess is that a non-compatible version is installed somewhere instead of using the version which is shipped along with Eclipse/PyDev.

Use virtualenv with Python with Visual Studio Code

I have a Python project and navigation/autocomplete work perfectly within files in the project. I want to specify a virtualenv so that navigation/autocomplete work with packages inside that virtualenv.
I tried this in settings.py, but navigation/autocomplete does not work. Also by setting "python.pythonPath":"~/dev/venvs/proj/bin/python killed the original navigation/autocomplete.
{
"python.autoComplete.extraPaths": [
"~/dev/venvs/proj",
"~/dev/venvs/proj/lib"
]
}
With the latest update to the extension all you need to do is just specify the "python.pythonPath" as follows.
The values for "python.autoComplete.extraPaths" will be determined during runtime, but you are still free to specify custom paths in there.
Please, remember to restart Visual Studio Code once the necessary changes have been made.
{
"python.pythonPath": "~/dev/venvs/proj/bin/python"
}
As of September 2016 (according to the GitHub repository documentation of the extension) you can just execute a command from within Visual Studio Code that will let you select the interpreter from an automatically generated list of known interpreters (including the one in your project's virtual environment).
How can I use this feature?
Select the command Python: Select Workspace Interpreter(*) from the command palette (F1).
Upon selecting the above command a list of discovered interpreters will be displayed in a quick pick list.
Selecting an interpreter from this list will update the settings.json file automatically.
(*) This command has been updated to Python: Select Interpreter in the latest release of Visual Studio Code (thanks #nngeek).
Also, notice that your selected interpreter will be shown at the left side of the statusbar, e.g., Python 3.6 64-bit. This is a button you can click to trigger the Select Interpreter feature.
Quite simple with the latest version of Visual Studio Code, if you have installed the official Python extension for Visual Studio Code:
Shift + Command + P
Type: Python: Select Interpreter
Choose your virtual environment.
Official site
With the latest Python extension for Visual Studio Code, there is a venvPath Setting:
// Path to folder with a list of Virtual Environments (e.g. ~/.pyenv, ~/Envs, ~/.virtualenvs).
"python.venvPath": "",
On Mac OS X, go to Code → Preferences → Settings and scroll down to Python Configuration.
Look for "python.venvPath: "", and click the pencil on the left-hand side to open up your user settings. Finally, add the path to where you store your virtual environments.
If you are using virtuanenvwrapper, or you have put all your virtual environment setting in one folder, this will be the one for you.
After you have configured "python.venvPath", restart Visual Studio Code. Then open the command palette and look for "Python: Select Interpreter". At this point, you should see the interpreter associated with the virtual environment you just added.
Another way is to open Visual Studio Code from a terminal with the virtualenv set and need to perform F1 Python: Select Interpreter and select the required virtualenv.
I put the absolute path of the virtual environment Python executable as well has the packages. I then restarted Visual Studio Code.
I am trying to get ${workspaceRoot} to avoid hardcoding absolute paths.
{
"editor.rulers": [80,100],
"python.pythonPath": "/home/jesvin/dev/ala/venv/bin/python",
"python.autoComplete.extraPaths": [
"/home/jesvin/dev/ala/venv/lib/python2.7",
"/home/jesvin/dev/ala/venv/lib/python2.7/site-packages"
]
}
I was able to use the workspace setting that other people on this page have been asking for.
In Preferences, ⌘+P, search for python.pythonPath in the search bar.
You should see something like:
// Path to Python, you can use a custom version of Python by modifying this setting to include the full path.
"python.pythonPath": "python"
Then click on the WORKSPACE SETTINGS tab on the right side of the window. This will make it so the setting is only applicable to the workspace you're in.
Afterwards, click on the pencil icon next to "python.pythonPath". This should copy the setting over the workspace settings.
Change the value to something like:
"python.pythonPath": "${workspaceFolder}/venv"
a) Modify Visual Studio Code default virtual env path setting. It's called "python.venvPath". You do this by going into code->settings and scroll down for python settings.
b) Restart VS Code
c) Now if you do Shift + Command + P and type Python: Select Interpreter
you should see list of your virtual environments.
It seems to be (as of 2018.03) in code-insider. A directive has been introduced called python.venvFolders:
"python.venvFolders": [
"envs",
".pyenv",
".direnv"
],
All you need is to add your virtualenv folder name.
On Mac OS X using Visual Studio Code version 1.34.0 (1.34.0) I had to do the following to get Visual Studio Code to recognise the virtual environments:
Location of my virtual environment (named ml_venv):
/Users/auser/.pyvenv/ml_venv
auser#HOST:~/.pyvenv$ tree -d -L 2
.
└── ml_venv
├── bin
├── include
└── lib
I added the following entry in Settings.json: "python.venvPath": "/Users/auser/.pyvenv"
I restarted the IDE, and now I could see the interpreter from my virtual environment:
I got this from YouTube Setting up Python Visual Studio Code... Venv
OK, the video really didn't help me all that much, but... the first comment under (by the person who posted the video) makes a lot of sense and is pure gold.
Basically, open up Visual Studio Code' built-in Terminal. Then source <your path>/activate.sh, the usual way you choose a venv from the command line. I have a predefined Bash function to find & launch the right script file and that worked just fine.
Quoting that YouTube comment directly (all credit to aneuris ap):
(you really only need steps 5-7)
1. Open your command line/terminal and type `pip virtualenv`.
2. Create a folder in which the virtualenv will be placed in.
3. 'cd' to the script folder in the virtualenv and run activate.bat (CMD).
4. Deactivate to turn of the virtualenv (CMD).
5. Open the project in Visual Studio Code and use its built-in terminal to 'cd' to the script folder in you virtualenv.
6. Type source activates (in Visual Studio Code I use the Git terminal).
7. Deactivate to turn off the virtualenv.
As you may notice, he's talking about activate.bat. So, if it works for me on a Mac, and it works on Windows too, chances are it's pretty robust and portable. 😀

Categories