Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode integrated terminal is not inheriting the virtualenv created by pipenv #660

Closed
tjaffri opened this issue Jan 30, 2018 · 6 comments
Closed

Comments

@tjaffri
Copy link

tjaffri commented Jan 30, 2018

Environment data

VS Code version: 1.19.3
Python Extension version: 3.6.4
Python Version: 3
OS and version: MacOS High Sierra 10.13.3
Pipenv version 9.0.1 installed via brew install pipenv

Actual behavior

When I create a virtualenv via pipenv (using pipenv shell) everything works in Terminal. Specifically, which python returns a path to python in the virtual env, e.g. /Users/tjaffri/.local/share/virtualenvs/hello-world-AxAtxCb5/bin/python.

Next, I launch vscode from inside the virtualenv using code .. I bring up the integrated terminal inside vscode and now it seems that vscode is not inheriting the virtualenv correctly. Specifically, which python returns a path to the system python /usr/bin/python.

Note that my vscode settings are completely empty (both workspace and user), so they are using default values for pythonPath etc.

When I try to do pipenv shell inside the vscode integrated terminal, I get the following output indicating that the virtualenv is active (but for some reason the python being used is still the system python)

Taqis-MacBook-Pro:hello-world tjaffri$ pipenv shell
Shell for /Users/tjaffri/.local/share/virtualenvs/hello-world-AxAtxCb5 already activated.
No action taken to avoid nested environments.

When I run the script in the integrated terminal via python3 hello.py (I have to use python3 rather than python due to the aforementioned issue) then there are problems with imported modules. Since the python3 being used is also the system python3 inside the integrated terminal, any modules installed via pipenv e.g. pipenv install beautifulsoup4 are not found and cannot be imported.

Note that when I run the same python3 hello.py outside the integrated terminal, i.e. in the pipenv shell in Terminal everything works correctly.

Expected behavior

Pipenv created virtual environment should inherit into the vscode integrated terminal, when vscode is launched from inside a virtual environment.

Specifically, the python from inside the virtual environment should be used, so that imported modules in the virtual environment resolve correctly.

Steps to reproduce:

  1. Open Terminal
  2. mkdir hello-world
  3. cd hello-world
  4. pipenv install
  5. pipenv shell
  6. which python (note the result, this will be python inside the virtualenv)
  7. code .
  8. launch integrated terminal
  9. which python (note the result, this will be the system python)
@brettcannon
Copy link
Member

We're tracking pipenv support over in #404 .

@tjaffri
Copy link
Author

tjaffri commented Jan 30, 2018

I'm sorry, I don't think this is the same as #404.

I believe this is going to happen for any virtualenv (even without pipenv involved).. This is preventing me from using pipenv even without official support from vscode.

How do people use vscode with virtual environments? Looks broken right now to me.

@DonJayamanne
Copy link

@tjaffri the problem is simple.
The terminal in VS Code does not inherit any environment variables or the like, treat it just like an terminal. Its just like activating an environment in your terminal and then starting a new terminal.

@tjaffri
Copy link
Author

tjaffri commented Jan 30, 2018

@DonJayamanne ah ok... thanks. That's helpful!

@ptiger10
Copy link

ptiger10 commented Jun 21, 2018

@tjaffri I was having the same issue that you originally described above, but couldn't decipher the answer for a long time. Posting here in case others are still struggling with this.

My interpretation (and the only way I have made pipenv work with VS Code) is that you must make sure that you have not already activated the virtual environment in your Terminal with pipenv shell before you type code . As long as this is the case, if you type pipenv shell in the integrated terminal after VS Code launches, it works perfectly and as expected.
This may not be obvious if:

  1. you pipenv shell by habit whenever you move into a project directory in Terminal (or, like me, you have autoenv trigger this automatically)
  2. you are migrating to pipenv from conda. In conda, if you have already activated a virtual environment in Terminal, then type code ., then activate the virtual environment again in the integrated terminal, you do not get any errors

Please correct me if you have a different interpretation of the answer, or found another way to make pipenv work with VS Code

@brettcannon
Copy link
Member

@ptiger10 mind opening a new issue about trying to detect when the user ran pipenv shell already? At worst we can leave it open for someone to contribute code to handle this case.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants