I'm trying to set up Python on Visual Studio Code on macOS. I have Python 2.7 and 3.5 interpreters installed on my computer. When I attempted to create my first Python file, the import modules were not found.
According to the various setup instructions I've seen, I'm supposed to be able to configure for my interpreter in "workspace settings." But I'm not sure I have a workspace, where to find it or how it is created.
When I go to the Command Palette and try to run Python: Select Workplace Interpreter I get an error that says:
Please open a workspace to select the Python Interpreter
When I go to menu File → Preference → Settings, the Workplace Settings tab is greyed out and inactive.
On this page, under User and Workspace Settings it says, "The workspace setting file is located under the .vscode folder in your project."
If a workspace is related to a project then how is a project created? After far as I can see, Visual Studio Code only has the menu option for menu File → New File, not New Project.
Where is the .vscode folder? How is it created?
A Visual Studio Code workspace is the directory which was opened by Visual Studio Code.
You should create a virtual environment and make Visual Studio Code using this virtual environment in user settings (file settings.json).
Example: Set "python.pythonPath": "${workspaceRoot}/venv/bin/python" in settings.json with venv is the virtual environment directory in the current workspace. Reopen Visual Studio Code.
Related
I am trying to learn more about Python in my spare time. I am a .net developer. I have downloaded a simple private open source project. The developer of the repository advised me to clone the repository and then run the project:
source .env/bin/activate
python3.6 ./project/main.py -c config.json
It works as expected. I am now thinking about debugging so that I can step through the code and debug it. Therefore I have downloaded Visual Studio Code and then installed the Python extension. I then start debugging and everything still works as expected. I opened the Visual Studio Code terminal and I see that Visual Studio Code runs the following:
bert#bertvb:~/myapp$ source /home/bert/myapp/.env/bin/activate
(.env) bert#bertvb:~/myapp$ /usr/bin/env /home/bert/myapp/.env/bin/python /home/bert/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/launcher 43369 -- /home/bert/myapp/myapp/main.py -c config.json
How does Visual studio Code know to run the first line i.e. to enter the virtual environment? I was advised that this would have to be run manually
Visual Studio Code automatically scans your workspace folder for any virtual environments. If it finds one, it will automatically enable it and select the Python interpreter installed in this virtual environment. If you want, you can disable this behaviour by setting the python.terminal.activateEnvironment option to false.
For more information, see the "Using Python environments in VS Code" documentation page, specifically the section "Where the extension looks for environments".
I was working with Visual Studio Code and Python 3.7 installed in the default installation directory (\users\user\appdata\roaming), but when I updated my Python installation to the last version, "3.9.1", and after deleting all previous versions, I decided to install it in another directory (C:\Python), my Visual Studio Code does recognize my Python installation as you can see below:
But when running Visual Studio Code again, it tells me that Pylint isn't installed, logical since I deleted everything from the previous installation.
The problem I'm facing right now is that when I install "pylint" it keeps installing in the default Python directory (\users\user\appdata\roaming) and telling me to add it to the "environment variables".
How can I configure Visual Studio Code to install all libraries/modules in my new Python directory (C:\Python)?
An environment variable tells your system where the python.exe file is located. Pylint doesn't know where python.exe is. Use these steps:
Right-clicking This PC and going to Properties
Clicking on the Advanced system settings in the menu on the left
Clicking on the Environment Variables button on the bottom right
In the System variables section, select the Path variable and click on Edit. The next screen will show all the directories that are currently a part of the PATH variable
Clicking on New and entering Python’s install directory
With a Python project opened, whenever I open the integrated terminal in Visual Studio Code it's automatically activating the virtual environment. I'm sure that's done by VS Code because I can find this command in the bash history of that terminal:
source <project-directory>/venv/bin/activate
I don't want the virtual environment to be activated. How can we prevent VS Code from doing that?
Add "python.terminal.activateEnvironment": false to your settings (globally to your user settings if you never want it, otherwise your settings.json file which is found in .vscode directory; this folder is found in your workspace directory if this is a per-workspace thing for you; docs).
If you open the vscode terminal first and then select python interpreter, then it won't activate the python environment. This answer works in vscode 1.44.0 with pyhton 3.8.1. I haven't tested with other versions. However, if you have any files or workspace open you need to close that first to close the python interpreter. You don't need to make any changes in settings.
Is it possible to automate or relocate a launch.json setting to the user settings so I do not have to edit the same line in each launch.json?
I have many Python modules in a directory outside of the Python Path and currently am having to edit each launch.json file to add the following line:
"env": {"PYTHONPATH": "\\my\\custom\\path"}
I tried adding the following line to User Settings, but it did not work:
"python.envFile": "\\path\\to\\python\\env\\file"
where the *.env contains the following:
"PYTHONPATH": "\\my\\custom\\path"
VScode I can open any python file that import from a custom path, right click and run it through VScode - I assume VScode is calling the native Python interpreter and seeing the PYTHONPATH environment variable.
Some Python environments (Spyder, PyCharm, etc) have a global setting for this, but I do not know how to accomplish this in VScode. Note VScode is consistent with PTVS; I have to edit the Search Paths for each Python solution in Visual Studio.
I don't know how to run the activate.bat in a Python Tools for Visual Studio Project. I have a directory environment in my project with my virtualenv. But, I don't know how I can run ./env/Scripts/activate.bat before the project run my main python script.
I usually point Visual Studio to a custom startup.py script, any other batch files I can then run using:
# startup.py
import os
import main.py # Whatever your main script is
os.system('activate.bat') # Call your batch files.
main.run() # Call whatever you need to from your main script.
In Visual Studio
Right click on project
Properties
General
Under Startup File, put startup.py (whatever)
Make sure your working directory is correct
I found that if :
main.py is set as Startup File,
in the Properties of the project -> Debug tab -> Interpreter Path field, I put the path C:...\env\Scripts\python.exe (i.e. the python executable of the virtualenv)
It works !
Python Tools for Visual Studio(PTVS) 2.0 is out now, in it you can add a virtualenv.
Open the Solution Explorer: View > Solution Explorer
Right click on 'Python Environments' and choose 'Add Virtual
Environment'
Here is a video showing how to do it.
Full support for Virtual Env is coming in PTVS 2.0 Beta/RTM. See http://pytools.codeplex.com for news/updates. Early support is in PTVS 2.0 Alpha, available now.