1

I've set up a Django (2.1.1) development environment using Vagrant and I've installed the Python extension for VS Code. I'm getting an error from pylint saying it can't import django.db, which makes sense since all of the Python modules are installed in the VM, and VS Code is using the Python environment on the host.

Does anyone know how (or even if) you can connect VS Code to the environment running inside of the VM so that linting and intellisense work?

darth_mall
  • 984
  • 8
  • 16

2 Answers2

0

This depends on how the VM and host are connected. If the user has permission to the interpreter you should be able to create a custom Python Environment in Visual Studio that uses that interpreter and its modules for intellisense.

enter image description here

This will get it to work from visual studio but in your code you may need to specify where you are importing the modules from. See Importing files from different folder

Dillon_Su
  • 91
  • 7
  • I'm not sure I understand what you mean by "If the user has permission to the interpreter". Are you saying that I need to create a shared folder from the guest OS to the host OS so that the interpreter that's installed on the guest is available to the host? Would the host computer even be able to execute that Python binary since they are different operating systems? – darth_mall Sep 07 '18 at 16:15
  • OK, I had this flipped around backwards thinking that the user(VM?) was trying to access python modules on the host. One thought would be to use a try and except on your imports so that no matter where the code is running it will have the correct modules. try: import Django except: import pip pip.main(['install','django']). https://stackoverflow.com/questions/12332975/installing-python-module-within-code – Dillon_Su Sep 07 '18 at 16:27
  • That kind of defeats the purpose of Vagrant, if I'm having to mirror the dev environment on the host. Not to mention that it's probably not a good idea to litter the application code with try-except blocks that install modules just for the dev environment. That seems like that would add some bloat to the code that's not necessarily useful to anyone else working on the code. Since intellisense is based on LanguageServer, I was hoping there'd be a way to start the LanguageServer for Python from the VM and have Code connect over a port, but I haven't found any docs to that effect. – darth_mall Sep 07 '18 at 16:39
0

Take a look at the extension Remote - SSH.

I opened my Vagrant virtual environment with configuration the command:

ssh -p 2222 vagrant@127.0.0.1 -i /Users/sb0y/vagrant/ubuntu/.vagrant/machines/default/docker/private_key

Works perfectly.

Sb0y
  • 65
  • 1
  • 8