0

I haven't touched python in a long time and I forgot how much of a pain importing can be.

So I'm using pylance and pipenv as my shell. I have pytest installed in my local env, I can run pytest from the command line and test my code. However, I can't import it in the same file I'm running my tests from. Any idea what might be causing that problem?

Python version 3.9

I have my tests in a separate file right below root. I don't have a init.py file in tests. I've read that that can cause problems.

Brandon-Perry
  • 366
  • 4
  • 18

2 Answers2

3

probably Pylance is having problems with the path for site-packages folder for your virtual environment and returns Import "pytest" could not be resolved, you need to set python path in this way:

"python.pythonPath": "/my/project/path/.venv/bin/python"
user11717481
  • 1
  • 9
  • 15
  • 25
  • Sorry again, I'm really bad at knowing about how python works on my machine. Is this a variable that I add to my pipfile? Or is this something that I need to find in a dir on my computer somewhere? – Brandon-Perry Dec 05 '21 at 03:14
  • this must be declared in settings to run the program using `visual studio` – user11717481 Dec 05 '21 at 03:16
  • Try add correspondingly to `/.vscode/settings.json` – user11717481 Dec 05 '21 at 03:29
  • I had tried adding to this to my User vscode settings (`~/.vscode/settings.json`) as a relative path but it wasn't working, but adding it to my project vscode settings (`/path/to/project/.vscode/settings.json`) as an absolute path worked great. – dantiston Nov 05 '22 at 02:32
0

I tried h. deville fletcher's answer and it didn't seem to work for me, but I could have messed up in there too. However, in the process, I remembered that I hadn't changed my interpreter. Once I changed the interpreter to my pipenv shell that fixed my problem.

Brandon-Perry
  • 366
  • 4
  • 18