I am currently using VS Code on a server (through SSH). Everything works fine, and I installed Python packages and work with Python notebooks.
Now, I want to login to the server (not a problem) and run the Python code I created on VSCode, rather than executing it remotely.
My main issue is that I am not sure how to activate the Python environment (if there is one) that VSCode server's run so that the code can execute.
Is that possible?
I see I have a .vscode directory in my home directory, and there are package installation there.
After connecting vscode remotely, you can use it as a regular vscode, which is no different from running Python files locally:
install python
install pylance extension
choose correct interpreter
edit your code and run the python file.
Running tests on my project code installed with pip in developer mode (pip install -e .) does work by executing pytest from the command line. However, running the same tests within the same Python virtualenv using the Test Explorer UI of Visual Studio Code does not work, Python raises ModuleNotFoundErrors.
I confirmed that my package is installed by pip list -v, which prints:
myproject 0.0.0 /path/to/myproject
Furthermore, I wrote failing test to get the Python path during test execution:
import sys
def test_some_test():
print(sys.executable)
print(sys.path)
assert False
This confirms that the Python executable is the one from the virtualenv where my project code is installed. But the sys.path differs between the Test Explorer run and the command line run. The Test Explorer run does not include the /path/to/myproject in the sys.path, whereas the command line run does. Obviously, if the project's path is included in the sys.path, the package can be imported. Why is it missing from the Test Explorer UI run? How to make the Test Explorer use the packages installed in develop mode with pip?
I should add that the modules I try to import in my tests are in a package themselves, called mylib. So the actual import in my test is like
from mylib import function_under_test
def test_function_under_test():
...
Update
This issue is fixed now. It just works.
Old Answer
Fiddling around with the issue more, I found that this is a bug in the Python extension for vscode, cf. issues
14579, and 14570 on github.com.
As a work-around one may either install the Little Fox Team Test Explorer UI extension, or an old version of the Microsoft Python extension (v2020.9.114305).
Starting Python BDD, using Behave in pycharm 2019.1 pro. Just started learning Python and Pycharm IDE.
Used a basic example to run a feature file with one scenario in it.
When I run using Pycharm Terminal, the scenario runs fine. When I do the same while right clicking on the Scenario in the editor, I get the message at run time, "No tests were found".
I have searched on SO but responses are for unit tests not for BDD tests.
I went into the run/debug configuration and have tried setup working directory to my feature folder or the project folder, but still the same issue.
I previously had python 3.7 but now using python 3.6 (both installed) and the interpreter is set to behave
I got it working but uninstalling pycharm, python 3.6 and python 3.7 folders completely (includuing hidden folders from private # root) from my machine, fresh install pycharm with python 3.6 and then behave. Tests work now.
No idea exactly why it happened except that it could be some conflicting config due to different python versions.
I am trying to solve partial differential equations with Python using FEniCS. I installed it with anaconda and conda-forge and to use it, I activate the fenicsproject environment
source activate fenicsproject
I run my scripts in jupyter (that works), but often it is more convenient to use VS Code for more elaborate code. When I run the scripts written in VS Code in the (built-in) terminal, they run without error as long as I have the fenicsproject environment enabled.
But in the editor I get a lot of errors like this
[pylint] Unable to import '...' [E0401]'
[pylint] Undefined variable '...' [E0602]
How can I get rid of those errors in the editor, so that the real errors can stand out.
What would be even better, make it that auto-complete and suggestions work for the packages like fenics, mshr etc.
According to the Python in Visual Studio Code docs, this is probably due to Visual Studio Code pointing at the wrong Python version.
1. Unable to import (pylint)
Scenario: You have a module installed, however the linter in the IDE is complaining about; not being able to import the module, hence error
messages such as the following are displayed as linter errors:
.. unable to import 'xxx' ..
Cause: The Python extension is most likely using the wrong version of Pylint.
Solution 1: (configure workspace settings to point to fully qualified python executable):
Open the workspace settings (settings.json)
Identify the fully qualified path to the python executable (this could even be a virtual environment)
Ensure Pylint is installed for the above python environment
Configure the setting "pythonPath" to point to (previously identified) the fully qualified python executable.
"python.pythonPath": "/users/xxx/bin/python" ```
Solution 2: (open VS Code from an activated virtual environment):
Open the terminal window
Activate the relevant python virtual environment
Ensure Pylint is installed within this virtual environment
pip install pylint
Close all instances of VS Code
Launch VS Code from within this terminal window
(this will ensure the VS Code process will inherit all of the Virtual Env environment settings)
A slight correction to "Solution 1" above: use
"python.defaultInterpreterPath": "/users/xxx/bin/python" ```
source: https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath
I'm using pylint in Visual Studio Code to develop a Google App Engine (GAE) Cloud Endpoint API in Python. I'm unable to resolve a lint error. I don't know what's causing the error, but at a guess, pylint cannot find the protorpc library?
The recommended fix in Troubleshooting Linting is to configure workspace settings to point to fully qualified python executable. I have done this, but the lint error remains.
protorpc itself is installed to:
~/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0/protorpc
...and this contains the remote.py module that cannot be imported:
__init__.py generate_python.py protojson.py transport.py
definition.py google_imports.py protourlencode.py util.py
descriptor.py message_types.py registry.py webapp
generate.py messages.py remote.py wsgi
generate_proto.py protobuf.py static
I've added this path to $PYTHONPATH (along with the kitchen sink):
export GOOGLE_CLOUD_SDK=~/google-cloud-sdk
export APPENGINE_PATH=$GOOGLE_CLOUD_SDK/platform/google_appengine
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib/googlecloudsdk
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib/googlecloudsdk/api_lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/platform/google_appengine/lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/platform/google_appengine/lib/protorpc-1.0/protorpc
The application runs locally and also when deployed, so this appears to be just a lint error, but it's frustrating that I can't solve it.
Using third-party libraries states:
The Python runtime in the standard environment includes the Python
standard library, the App Engine libraries, and a few bundled
third-party packages.
Because of this, I assumed 'the App Engine libraries' includes protorpc, but I'm unsure. Moreover, Adding the Cloud Endpoints Frameworks library to the sample API only requires google-endpoints be installed to the app's lib directory:
pip install -t lib google-endpoints --extra-index-url=https://gapi-pypi.appspot.com/admin/nurpc-dev --ignore-installed
My point is, I don't think I've not installed something, and I don't think I'm missing anything in my (web) app's lib directory.
Changing the library path worked for me. Hitting Ctrl + Shift + P and typing python interpreter and choosing one of the available shown. One was familiar (as pointed to a virtualenv that was working fine earlier) and it worked. Take note of the version of python you are working with, either 2.7 or 3.x and choose accordingly
I was facing same issue (VS Code).Resolved by below method
1) Select Interpreter command from the Command Palette (Ctrl+Shift+P)
2) Search for "Select Interpreter"
3) Select the installed python directory
Ref:- https://code.visualstudio.com/docs/python/environments#_select-an-environment
Open the settings file of your Visual Studio Code (settings.json) and add the library path to the "python.autoComplete.extraPaths" list.
"python.autoComplete.extraPaths": [
"~/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2",
"~/google-cloud-sdk/platform/google_appengine",
"~/google-cloud-sdk/lib",
"~/google-cloud-sdk/platform/google_appengine/lib/endpoints-1.0",
"~/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0"
],
For your case, add the following code to vscode's settings.json.
"python.linting.pylintArgs": [
"--init-hook='import sys; sys.path.append(\"~/google-cloud-sdk/platform/google_appengine/lib\")'"
]
For the other who got troubles with pip packages, you can go with
"python.linting.pylintArgs": [
"--init-hook='import sys; sys.path.append(\"/usr/local/lib/python3.7/dist-packages\")'"
]
You should replace python3.7 above with your python version.
Spent hours trying to fix the error for importing local modules. Code execution was fine but pylint showed:
Unable to import '<module>'
Finally figured:
First of all, select the correct python path. (In the case of a virtual environment, it will be venv/bin/python). You can do this by hitting
Make sure that your pylint path is the same as the python path you chose in step 1. (You can open VS Code from within the activated venv from terminal so it automatically performs these two steps)
The most important step: Add an empty __init__.py file in the folder that contains your module file. Although python3 does not require this file for importing modules, I think pylint still requires it for linting.
Restart VS Code, the errors should be gone!
I've not played around with all possibilities, but at least I had the impression that this could be a python version related issue. No idea why, I just trusted my gut.
Thus I just changed the pythonPath to python3 (default: python):
"python.pythonPath": "python3"
I reinstalled the dependencies (including pylint!!!) with
pip3 install <package> --user
... and after restarting vs code, everything looked fine.
HTH Kai
I was still getting these errors even after confirming that the correct python and pylint were being used from my virtual env.
Eventually I figured out that in Visual Studio Code I was A) opening my project directory, which is B) where my Python virtual environment was, but I was C) running my main Python program from two levels deeper. Those three things need to be in sync for everything to work.
Here's what I would recommend:
In Visual Studio Code, open the directory containing your main Python program. (This may or may not be the top level of the project directory.)
Select Terminal menu > New Terminal, and create an virtual environment directly inside the same directory.
python3 -m venv env
Install pylint in the virtual environment. If you select any Python file in the sidebar, Visual Studio Code will offer to do this for you. Alternatively, source env/bin/activate then pip install pylint.
In the blue bottom bar of the editor window, choose the Python interpreter env/bin/python. Alternatively, go to Settings and set "Python: Python Path." This sets python.pythonPath in Settings.json.
The simplest solution is to create a .env file in your project root with this content:
PYTHONPATH=.
You don't need __init__.py files. It works even if your code is in src dir, and unit tests in tests subdirs. This helped pylint and pytest to find all the modules.
For more info, see https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file
The visual studio default setting should be the same as the interpreter path.
Change VS code default setting: windows: File > Preferences > Settings
{
"python.pythonPath": "C:\\Users\\Anaconda3\\pythonw.exe",
"workbench.startupEditor": "newUntitledFile"
}
Find the right interpreter:
windows: Ctrl+Shift+P->select interpreter:
the path of that interpreter should be same as the version you are working on.
First I will check the python3 path where it lives
And then in the VS Code settings just add that path, for example:
"python.pythonPath": "/usr/local/bin/python3"
I resolved this by adding the protorpc library to the $PYTHONPATH environment variable. Specifically, I pointed to the library installed in my App Engine directory:
export PYTHONPATH=$PYTHONPATH:/Users/jackwootton/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0
After adding this to ~/.bash_profile, restarting my machine and Visual Studio Code, the import errors went away.
For completeness, I did not modify any Visual Studio Code settings relating to Python. Full ~/.bash_profile file:
export PATH=/Users/jackwootton/protoc3/bin:$PATH
export PYTHONPATH=/Users/jackwootton/google-cloud-sdk/platform/google_appengine
export PYTHONPATH=$PYTHONPATH:/Users/jackwootton/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/jackwootton/google-cloud-sdk/path.bash.inc' ]; then source '/Users/jackwootton/google-cloud-sdk/path.bash.inc'; fi
# The next line enables shell command completion for gcloud.
if [ -f '/Users/jackwootton/google-cloud-sdk/completion.bash.inc' ]; then source '/Users/jackwootton/google-cloud-sdk/completion.bash.inc'; fi
I find the solutions stated above very useful. Especially the Python's Virtual Environment explanation by jrc.
In my case, I was using Docker and was editing 'local' files (not direcly inside the docker).
So I installed Remote Development extension by Microsoft.
ext install ms-vscode-remote.vscode-remote-extensionpack
More details can be found at https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
I should say, it was not easy to play around at first.
What worked for me was...
1. starting docker
2. In vscode, Remote-container: Attach to running container
3. Adding folder /code/<path-to-code-folder> from root of the machine to vscode
and then installing python extension + pylint
Another solution could be generating a pylintrc in project location using command: (this command will by default print the content to terminal and not actually create a file, you need to dump output to .pylintrc)
pylint --generate-rcfile
and then update key init-hook= using:
import sys; sys.path.append("your_project_location")
In my case, I tried flake8, bandit, didn't work, eventually I uninstalled the extension called python (pylance) and everything worked perfectly.
I got the same error on my vscode where I had a library installed and the code working when running from the terminal, but for some reason, the vscode pylint was not able to pick the installed package returning the infamous error:
Unable to import 'someLibrary.someModule' pylint(import-error)
The problem might arise due to the multiple Python installations. Basically you have installed a library/package on one, and vscode pylint is installed and running from another installation. For example, on macOS and many Linux distros, there are by default Python2 installed and when you install Python3 this might cause confusion. Also on windows the Chocolatey package manager might cause some mess and you end up with multiple Python installations. To figure it out if you are on a *nix machine (i.e., macOS, GNU/Linux, BSD...), use the which command, and if you are on Windows, use the where command to find the installed Python interpreters. For example, on *nix machines:
which python3
and on Windows
where python
then you may want to uninstall the ones you don't want. and the one you want to use check if the package causing above issue is installed by
python -c "import someLibrary"
if you get an error then you should install it by for example pip:
pip install someLibrary
then on vscode press ⌘⇧P if you are on a mac and CtrlShiftP on other operating systems. Then type-select the >python: Select Interpreter option and select the one you know have the library installed. At this moment vscode might asks you to install pyling again, which you just go on with.
I resolve this error by below step :
1 : first of all write this code in terminal :
...$ which python3
/usr/bin/python3
2 : Then :
"python.pythonPath": "/users/bin/python",
done.
I had same problem for pyodbc , I had two version of python on my Ubuntu (python3.8 and python3.9), problem was: package installed on python3.8 location but my interpreter was for python3.9. i installed python3.8 interpreter in command palette and it fixed.
Other solutions not working for me (multiple workspaces)
Create ~/.vscode/workspace.env with following content
PYTHONPATH=$PYTHONPATH:/Users/jackwootton/protoc3/bin
Go to Workspace Settings: ⌘ / Ctrl⇧P → Workspace Settings.
Add next config line there:
"python.envFile": "/Users/jackwootton/.vscode/workspace.env",
Reload Window.
This solution better than #Jack, because it doesn't modify a global variable for all system, just for your project workspace.
In my case, the packages got installed in the global Python installation path and not in the venv, even though I had the virtual environment activated at the time of installation.
I could see this when I switched to the base environment (by a click on the bottom left status field of the chosen interpreter) and saw that the package could be imported.
I could only solve this by removing the venv and installing it again.
pylint is run in the vscode terminal, and its output transferred to the editor. Therefore the terminal environment settings need to match those in the command line outside vscode. Specifically, in settings.json:
"terminal.integrated.env.linux": {
"PYTHONPATH": "/path/to/library/files/you/want/pylint/to/FINALLY/EXPLETIVE/FIND"
},