0

I am new to VS Code. I have created a virtual environment and selected that as the interpreter, in which I have run

pip install -r requirements 

from the terminal which allowed for pandas to be installed in that virtual environment (I believe based on the docs

From that directory in the terminal, I ran

python myscript.py

It works. Generates the output as it should.

I then try to run the same script in the window above and I get the error, no module named pandas. I have not changed the interpreter. enter image description here UPDATE When closing the terminal and then attempting to run the code from the explorer/deug window, I received a failure to run scripts notice. I therefore (based on this post changed my Execution Policies in Windows Shell (separate, not in VS terminal), and now it at least goes into the environment, however I still get the same error, pandas unknown. enter image description here

UPDATE 2 I noticed in the tutorial that they created the directory and then created the environment within that. I moved my code to be under the virtual environment I created and then it works, both from the terminal and from the debug/explorer window. However here I noticed that the intrepetor is not characterised as a virtual environment but instead looks like the standard version- now there is no option for me to select working_env as an interpretor.
Running pip freeze shows the latest version of pandas which is not what I have installed elsewhere for instance. So this works, but I am now not sure if this is really how I should be using virtual environments.

**UPDATE 3 For whatever reason I can now update the interpreter. When I do however I am back to where I was. I can run the program in the terminal with the interpreteter selected, but running from the debug window I get the error that pandas is missing. Trying to install pandas I am told the requirement is satisfied. Running pip freeze confirms that this is installed. I have the feeling I am somehow installing pandas not in the environment I want enter image description here

enter image description here

What have I missed in my vscode setup?

James Oliver
  • 547
  • 1
  • 4
  • 17
  • 1
    Click on the `Python 3.9.5 64-Bit` and find the Interpreter. Select `python.exe` in the directory with name of the VENV – Bhavyadeep Yadav Jun 06 '21 at 11:55
  • Thanks @Bhavyadeep. This indeed got me to change to the virtual env / interpreter. However I am now back to where clicking on the play button it doesn't acknowledge pandas has been installed (detailed more in my update 3). I think I am somehow installing in main python and not in my environment. – James Oliver Jun 06 '21 at 12:49
  • 1
    Your venv is not activated in your VS terminal. Select the venv in the lower left hand corner and upper right hand corner (kernel for jupyter). In the VS terminal you should see something like `(working_env) PS D:\...` and a quick python test should confirm you have pandas installed. – Davis Jun 06 '21 at 14:36
  • 1
    @JamesOliver Select the Venv, close the terminal and open it again. Then install pandas again – Bhavyadeep Yadav Jun 07 '21 at 07:19

1 Answers1

0

Per both the comments, I had a couple of issues here.

  1. For whatever reason during update 2, I couldn't see the interpreter when I was in the folder initially but it was there. It is noted in the docs that this can take a while. Regardless, it must show in the bottom left that you are in the interpreter

  2. Using powershell, I needed to change the execution policy to be unrestricted.

    Set-ExecutionPolicy Unrestricted -Scope CurrentUser

  3. Then I hadn't installed pandas in the virtual environment AND with the interpreter. Therefore

    . ..\Scripts\activate.ps1 activated the working environment

    python -m pip install -r requirements.txt

installed my packages

Running my script was achieved by

python .\myscript.py

and that also runs with the play button (though here I had to press Ctrl+Sft+P type Terminal: Select Default Shell, ensure the terminal was powershell & restart)

James Oliver
  • 547
  • 1
  • 4
  • 17