简体   繁体   中英

Start PyLint from correct anaconda environment in Visual Studio Code

I'm trying to make PyLint automagically use correct conda environment inside vscode but still getting import errors: [pylint] E0401:Unable to import 'django' , although:

  • I'm starting vscode from correct environment. [1]
  • I have installed Python extension. [2]
  • I have set correct python.path . [3]

You have to install pylint in this conda environment.

  1. Activate given environment with activate env_name (Windows) or source activate env_name .

  2. Install pylint in this environment:

     conda install pylint # or 'pip install pylint'
  3. Finally, restart vscode.

Source: https://github.com/DonJayamanne/pythonVSCode/wiki/Troubleshooting-Linting

Even after correct installation of python and pylint faced the issue due to wrong configuration of pylintArgs in visual studio code.

With below user settings got the issue resolved

"python.linting.pylintArgs": [
        "--load-plugins", 
        "pylint_django"
    ] 

You just need to make sure your working within the same virtual environment you created with conda itself. Basically you need to enable the "Python: Enable Linting command". Normally at the bottom left hand corner, VS Code will tell you what environment you are working within. If you open VS Code via the anaconda-navigator GUI (even if you opened it with the right corresponding environment). VS Code, by default opens the editor using the (base) conda environment. This could perhaps be the confusion. The Linting is ironically doing just what it is intended to do. Via that prompt in that bottom left hand corner, just change the environment to the one you custom made for your project. Close the editor, and completely shut down the anaconda-navigator GUI. Just update & upgrade your system using the terminal (if you are using a Linux-based OS). Quickly update conda, just to make sure nothing goes wrong. Restart anaconda-navigator, and open VS Code. If you run the internal terminal of the editor, you will see it open via the (base) environment, and automatically it will change to the correct corresponding conda environment, doing so by itself;

source /home/user/anaconda3/bin/activate
(base) user@machine:~$ source /home/user/anaconda3/bin/activate
(base) user@machine:~$ conda activate your_env
(your_env) user@machine:~$  

PS - I personally find using the Anaconda Navigator makes my life a lot more easier.

Maybe for further reading just swizz through VS Code Documentation https://code.visualstudio.com/docs/python/linting . HapPy HaCkiNG!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM