pipenv icon indicating copy to clipboard operation
pipenv copied to clipboard

Error when running pytest using subprocess in a virtual environment

Open Matjoman opened this issue 2 years ago • 6 comments

Hi, I have an issues that I have been trying to resolve for quite some time.

I have a automated test for an API where I start the server with the following command.

  def setup_method(self):
        # start server
        port = str(get_free_port())
        child_env = os.environ.copy()
        child_env['PORT'] = port

        # proc = subprocess.Popen(
        proc = subprocess.run(
            ['python', 'src/server.py'],
            env=child_env,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            # shell=True
        )

and then I run a bunch of different tests against it.

The tests has been working for some years both on a messy Windows environment and in a clean docker image.

What now has begun to happen is that the server fails to start due to the fact that it cannot find the package Waitress which is installed and accessible within the environment.

I have managed to find out a remedy but I have no clue why this works and why pipenv is behaving in this way. When I install my environment Pipenv does not copy Python.exe as is from where I have told it to get it. Instead there is a much larger Python.exe (Hybrid) located in the environment/Scripts. If I replace this file with Python.exe, Python3.dll and Python310.dll in the created environment it manages to run the test within the activated shell.

When I have debugged I and looked at the child_env I get from the above code have found that the hybrid version has different paths in a key called 'LIBRARY_ROOTS'. The hybrid version has /Scripts but the older version has /Lib/site-packages also.

If I look at older environments I can see that they have the Python + 2 dll file setup but when I make a copy of a project with an old environment and install directly from the Pipfile.lock I now get a larger hybrid Python.exe version and the test fails.

I have tried to install 6 different versions of Pipenv but nothing helps.

I have included a minimal project, basically a server that only has one endpoint and a version of the failing test. This fails on my computer but I have not tested it on any other.

Summary: I am at a loss with this weird behavior. My code runs well but it is the test that is spawned by subprocess that has begun failing. This seems to be caused by a hybrid Python.exe instead of Python.exe + 2*dll files in the created environment. Pipenv_bug.zip

Best regards, Mattias

Matjoman avatar Jun 21 '23 18:06 Matjoman

What pipenv commands are you running? -- I see the Pipfile in the zip, but I am not sure how you are invoking things.

matteius avatar Jun 21 '23 21:06 matteius

What pipenv commands are you running? -- I see the Pipfile in the zip, but I am not sure how you are invoking things.

pipenv install --python "path to Python.exe" ,unless you have the specified Python version as your system version.

Matjoman avatar Jun 22 '23 06:06 Matjoman

Could someone explain why Pipenv creates a new Python.exe instead of copying the one specified and when did this behavior change? Or is that with Cpython that change is?

Matjoman avatar Jun 26 '23 07:06 Matjoman

Maybe virtualenv does -- I am not sure.

matteius avatar Jun 26 '23 11:06 matteius

Is it possible the python you are invoking in the subprocess is not the virtualenv python where waitress is installed?

matteius avatar Jun 28 '23 22:06 matteius

That might be the case, but since it has worked before I don't know why that would have changed but I will look in to that.

Matjoman avatar Jun 29 '23 08:06 Matjoman