VScode Integrated Terminal Virtual Environment

I am trying to figure out how to get the terminal in VScode to activate the virtual environment automatically. Thus far I have followed along without issues in Part I of the Django course but now I seem to have hit a wall.

First I tried following Mosh’s steps by locating the virtual environment location in terminal on the Mac using pipenv --venv and this returned the location as /Users/lewpiper/.local/share/virtualenvs/storefront-7UYLLetM on my Mac. I then tried to to use this in the command palette to select the python interpreter and it didn’t seem to work. I saw I had no VScode JSON file like the videos showed.

Then I tried to edit the workspace settings in VScode and add the following "python.pythonPath": "/Users/lewpiper/.local/share/virtualenvs/storefront-7UYLLetM" to match Mosh’s file structure and again I am running into issues.

Now when I click play in VScode I get the following message “ImportError: Couldn’t import Django. Are you sure it’s installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?”

It seems like a few other people have struggled with this as well, but I haven’t been able to come to a solution. Any help or direction to guide me in the right direction would be very much appreciated.

Try ‘pipenv shell’ in the terminal. That should work.

If not, then

on Windows: python interpreter-> whatever version you have

Then run pipenv shell

Good luck.

I honestly think this is just not a feature of VSCode (yet - I would not be surprised if there was a feature added for this sooner or later).

Lots of details here:

To sum it all up

  • Do choose or set the correct interpreter using the command palette and Python: Select Interpreter command. This is remembered the next time you open the file, so far as I can tell.
  • Do create a launch profile so that you can launch your application and then it does activate the environment for you (if necessary) when you use the launcher to start the program (Debugging in Visual Studio Code). If this is for Django, be sure to choose the Django launch profile, not just the Python launch profile.
  • You can “trick” VSCode into activating the environment by right-clicking in the code editor and choosing “run python file in terminal” (but this will run the file, which may or may not succeed and may or may not be a bother). Similarly, you can launch your program once, if you have created a launcher.
  • You can also use the “new terminal” command from the menus or via a shortcut key to start a new terminal, which is activated in the correct environment. This can also be a bother if you already have a terminal open (now you have two, one that you probably want to close).
  • You can do some fiddle faddling to have VSCode create a new terminal for you somehow - that would be a clever trick and I can’t say I know how to do it.
  • You can do what I do - just activate your terminal environment the old fashioned way with a command in the terminal :slight_smile: For this however, you can probably define and use command aliases to create a very short command to make it easier, if you want to (this is also something I do - but since I’m on linux the method I use probably doesn’t apply).

Well, that’s all I can figure out - as you can see I think for now there is not a real solution just some things you can do to help the situation. However, its a small price to pay - a single command in your terminal and you can happily use VSCode for hours after that.