1

Trying to setup Visual-Studio-Code and running into issues configuring virtualEnv properly. Read everything i could find online and tried different solutions: none worked so far. MAYBE i am just not getting the documentation and everyone else gets it.

  1. Proof that the virtualEnv as well as the package oauth2client exists.

enter image description here

  1. Path of the virtualEnv

enter image description here

  1. Setup in Visual Studio Code (yes i restarted after saving the settings).

enter image description here

  1. oauth2client can't be found. As you can see in the corner the Python interpreter was rightfully found.

enter image description here

I hope that one of you guys can spot my mistake.

Update:

So thanks to mattmc3 i figured it out. Was using the "Code Runner" Plugin for running the python code. I missed setting up the right Interpreter for this plugin - used still the default one.

enter image description here

5
  • 1
    How did you run your Python script? Did you right click in the "Explorer" and click "run file in terminal"? If you did you should see which python bin is used. Did you launch the debugger screen and click the play button (which requires you to initialize a launch.json file)? Or do you have a build action in tasks.json that is misconfigured? "py3" in your last screenshot indicates that you have the venv set up properly for VSCode, so your problem seems to be in how you launch your script. Please detail how. Commented Dec 9, 2017 at 22:03
  • omg i am such an idiot! was running the script via the small Play button in the right top corner... When i run it via "right-click" > "Run Python File in Terminal" or in the Debugger it works perfectly. Do i miss some settings in order to use the "Run Code" command as well ? Commented Dec 9, 2017 at 22:14
  • I don't have the "play triangle" in the top right, so I have to imagine you are using some sort of plugin I don't have. For Python, I use the MS one: marketplace.visualstudio.com/items?itemName=ms-python.python . Any indication what that plugin is? Commented Dec 9, 2017 at 22:18
  • "Code Runner" is the plugin. I followed some youtube video explaining how to setup Visual Studio Code (incl. install Code Runner) - so i actually never realised that the small Play Button is not a default function of the basic setup... Many Thanks for your help - much appreciated! wasted quite some hours on that issue Commented Dec 9, 2017 at 22:22
  • 1
    Awesome! I created an answer detailing the dialog we had, if you wouldn't mind marking it as resolved. Best of luck in your Python endeavors with VSCode. It's how I have done all my Python development and I love it, so keep at it. Commented Dec 9, 2017 at 22:28

2 Answers 2

2

Your final screenshot indicates that you are using your venv properly, which you named "py3". There are lots of ways to launch your Python script from VSCode:

  • Right click the file in VSCode's file Explorer and select "Run Python File in Terminal"
  • Click the debugger menu and set up a launch.json file to ensure you are using your venv. This method has the added advantage of allowing you to step through your code.
  • Set up a tasks.json file to create a build task using your preferred venv.

Based on the back-and-forth comments we exchanged, it appears that you did not launch using any of these standard methods, which could explain why your venv is not being picked up.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is the stepbystep how to create the Virtual environment in Visual Studio Code folder: I used Powershell (Administrator mode):
1. I create a VSCode folder - "D:\Code_Python_VE" where I want to create Virtual environment.
2. Next I type the command - "pip3 install virtualenv". (D:\Code_Python_VE> pip3 install virtualenv ) 3. D:\Code_Python_VE> python3 -m venv project_env
4. D:\Code_Python_VE>project_env\Scripts\activate.bat
5. D:\Code_Python_VE> ls - This will list a new directory "project_env".
6. D:\Code_Python_VE> code . This will start Visual Studio Code. Make sure the command is (code .).
7. Create launch.jason with following content:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "python",
            "request": "launch",
            "name": "Python: Current File (Integrated Terminal 1)",
            "program": "${file}"
        },
        {
            "name": "Python: Current File (Integrated Terminal 2)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
    ]
}

(Please search how to go to Debug window and Add new Configuration in VS Code).

  1. Press F1 in Visual studio code and the command pallet will open - Select Python Interpreter and select the virtual environment project_env.
  2. Add test.py file with one statement print("Hello World").
  3. Run this program.
  4. In Visual studio Code terminal -
    (project_env) d:\Code_Python_VE>python -m pip install --upgrade
    I hope this helps.

1 Comment

Thanks for this, step 8 helped me with the problem I was having, similar to OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.