0

I am working with ros on ubuntu 16.04. Because of this I am working with a virtual environment for python 2.7 and the ros python modules (rospy for example). The "python.pythonPath" is set to the virtual environment and the ros modules are linked through "python.autoComplete.extraPaths".

This leads to the issue where the python linter raises an error for import rospy claiming that it can not import it. However, the python intellisense is still able detect and help with the rospy module (which makes sense due to the python.autoComplete.extraPaths setting).

Is there a way to include the extra paths for autoComplete for the linter as well? At this point, no longer including the virtual environment for the python path is not a desirable option so I am looking for a way to have the linter include the extra paths for ros python modules and the modules in the virtual environment.

Nick
  • 1
  • 1

2 Answers2

0

It depends on the linter, but you can pass extra arguments to linters through your settings if the linter supports a way to pass extra directories, e.g. python.linting.flake8Args.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
0

I experienced the mentioned linting error despite using the VSCode ROS extension and thus having settings.json automatically configured with "python.autoComplete.extraPaths": ["/opt/ros/melodic/lib/python2.7/dist-packages"] and my workspace paths.

I kept trying to add the paths with an init-hook to python.lintin.pylintArgs as suggested here which never worked for me.

Adding

[MASTER]
init-hook='import sys; sys.path.append("/opt/ros/melodic/lib/python2.7/dist-packages")'

(and other paths if applicable) to ~/.pylintrc finally did the trick.

As echo $PYTHONPATH was also set correctly by the extension,

[MASTER]
init-hook="from pylint.config import find_pylintrc;
import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

even seems to even load all paths automatically.

EDIT:

On Ubuntu 18.04 with ROS Melodic and the latest VS Code with ROS, Python and Pylint extension, resolving rospy worked out of the box without configuring any .pylintrc when opening the workspace root folder in VS Code after it was built at least once with catkin_make.

To import custom Python modules from <ws>/src/<package-name>/src, I had to add the relative path src/<package-name>/src to "python.autoComplete.extraPaths".

(Make sure to provide <ws>/src/<package-name>/setup.py and an empty <ws>/src/<package-name>/src/<package-name>/__init__.py as discussed here.)

F1iX
  • 823
  • 6
  • 12