Python Forum
Pytest Installed, but VS Code Won’t Access Pytest
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pytest Installed, but VS Code Won’t Access Pytest
#1
Hi,

Sorry, I understand questions about installing packages and problems with path directories are common, but I've tried everything and really need help.

I’m starting to learn how to write tests for my code.

I’ve installed pytest on my device using pip.

But VS Code won’t access it. I assume it’s something to do with the path. I’ve tried using different IDEs, but I can’t find one that will let me use pytest without installing it.

When I type "import pytest", VS Code just says:

Quote:"pytest" is not accessed Pylance

I have added a path by going into the settings and adding a path to Python > Testing: Pytest Path. But it still doesn't work...

I've tried two possible paths:

C:\Users\user\anaconda3\Lib\site-packages\pytest
C:\Users\user\AppData\Local\Programs\Python\Python311\Scripts\pytest.exe
C:\Users\user\AppData\Local\Programs\Python\Python311\Scripts\pytest

I'm obviously doing some newbie error... e.g. I took the .exe off, but it still won't access it. But I can't find any other possible path...

The second possible path I tried was the answer I got from asking Command Prompt "where pytest".

Any help would be greatly appreciated.

Thanks.
Reply
#2
Don't mess with path,just make sure when in VS Code down in right corner choice right Python interpreter.
Look at this post
Most only make sure that python and pip is in OS path,and work anywhere from cmd,or i would advise use cmder(full)
G:\div_code
λ python -V
Python 3.11.3

G:\div_code
λ pip -V
pip 23.1.2 from C:\python311\Lib\site-packages\pip (python 3.11)

G:\div_code\egg\prosject
λ pip install pytest --upgrade
Requirement already satisfied: pytest in c:\python311\lib\site-packages (7.4.2)
Requirement already satisfied: iniconfig in c:\python311\lib\site-packages (from pytest) (2.0.0)
Requirement already satisfied: packaging in c:\python311\lib\site-packages (from pytest) (23.1)
Requirement already satisfied: pluggy<2.0,>=0.12 in c:\python311\lib\site-packages (from pytest) (1.3.0)
Requirement already satisfied: colorama in c:\python311\lib\site-packages (from pytest) (0.4.6)
So in this case it should be 3.11.3 -64bit down in right corner,i have pytest installed to Python 3.11.3 then no error messages.

To make your error i can activate environment as posted in link
In this environment i do not have pytest install,then will Pylance complain.
Error:
Import "pytest" could not be resolved
If i install pytest in this virtual environment,then error goes away.
Reply
#3
So the issue is that pytest and the version of python I am using are not installed in the same directory?

What command should I use to download pytest to a particular folder that contains python? e.g. like in the example you showed above?
Reply
#4
It's also showing the same error when I try to import the test file.

e.g. if the test file were calculation.py, when I type import calculation, it comes up with the same error, namely that import "calculation" could not be resolved.
Reply
#5
(Sep-12-2023, 02:56 PM)AstralWeeks Wrote: What command should I use to download pytest to a particular folder that contains python?
You shall not download at all,this is all done bye pip.
Open cmd and test python and pip command.
C:\Users>cd ..

C:\>python -V
Python 3.11.3

C:\>pip -V
pip 23.1.2 from C:\python311\Lib\site-packages\pip (python 3.11)

C:\>
So it most work like this,if not you most fix you environment variables path.
Then will pip install pytest --upgrade download and install automatic to this Python 3.11.
When this work on command line as posted before in VS Code down in right corner Python interpreter(choose same version).

Quote:e.g. if the test file were calculation.py, when I type import calculation, it comes up with the same error, namely that import "calculation" could not be resolved
That's different when use own modules file most be in Python sys.path or run from same folder as code.
Reply
#6
Would you mind sharing all the code?

The error you are describing is relative to when you import something but don't use it at all in your code.
snippsat likes this post
Reply
#7
(Sep-12-2023, 07:40 PM)carecavoador Wrote: Would you mind sharing all the code?

The error you are describing is relative to when you import something but don't use it at all in your code.

Sure.

I've not actually written my code yet, I've just written a standard arithmetic function to test if everything is set up correctly or not

import sum
import pytest

def test_add():
    assert sum.add(2, 5) == 7

    return None
The sum.py file is just:

def add(a, b):
    return a + b
I've ensured that both files are saved in the same folder. I can import the sum.py file now, after I've made sure both files are in the Explorer at the left side of VS. But I still can't get it to operate pytest.

I've made sure that all the versions are up-to-date.
deanhystad write Sep-13-2023, 02:43 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#8
I should add that the code is indented correctly. The indents disappeared when I copied and pasted the code.
Reply
#9
You don't use pytest like this,pytest is primary a tool you use on command line.
So carecavoador was right in his assuming.
Here you import pytest,but never use in code then then Pylance will complain.
Error:
pytest" is not accessedPylance
Also sum is a named used by Python,so do not name a file sum.py.
import sum1
import pytest
 
def test_add():
    assert sum.add(2, 5) == 7 
    return None
Here a quick tutorial.
[Image: RSgecS.png]
So all work from command line,see that i made a file inside_run.py
import pytest

pytest_args = [
    'C:/code/pro_py/test_add.py', '-v'
]
pytest.main(pytest_args)
This i what needed to run pytest not by using command line.but eg inside VS Code.
I only use pytest from command line(cmder) never from code inside VS Code.
Aslo code . is convenient to start VS Code from folder you are in.
Reply
#10
Thanks for all your help. This is all new to me... I just finished a small course that introduced pytest, but didn't teach us to use the command line to run it...

I'll have to read through this and play around with it. I'll get back to you if I get stuck at any point.

Thanks Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytest mocks anthonyrmoss78 0 454 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,576 Jun-27-2021, 08:30 PM
Last Post: Hpao
  How to run a pytest test for each item in a list arielma 0 2,379 Jan-06-2021, 10:40 PM
Last Post: arielma
  Noob warning: trying to use pip to install pytest and pep8 in Command Prompt adifrank 4 5,345 Dec-20-2020, 04:23 AM
Last Post: adifrank
  Error in code tensorflow is installed ErnestTBass 5 6,518 Nov-04-2020, 03:43 PM
Last Post: snippsat
  capture pytest results to a file maiya 2 5,857 Oct-17-2020, 03:42 AM
Last Post: maiya
  pytest, parametrize, neasted list niski1996 0 1,583 Jul-21-2020, 10:36 PM
Last Post: niski1996
  Erreur Code access violation reading 0x00000004 yan_mhb 0 2,336 Jul-10-2020, 02:28 PM
Last Post: yan_mhb
  how to generate html report for each folder using pytest ktrsarath 0 2,205 Jun-30-2020, 05:14 AM
Last Post: ktrsarath
  Pytest and rootdirectory Master_Sergius 4 4,698 Jun-01-2020, 05:05 PM
Last Post: Master_Sergius

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020