Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and run may use different python interpreters #335

Closed
saskeli opened this issue May 20, 2020 · 5 comments · Fixed by #505
Closed

Test and run may use different python interpreters #335

saskeli opened this issue May 20, 2020 · 5 comments · Fixed by #505
Assignees
Milestone

Comments

@saskeli
Copy link

saskeli commented May 20, 2020

Description

If using a virtual environment python interpreter for completing exercises, one may run into problems where the interpreter set in VS Code is used for executing the python code, but tests run using the default python interpreter of a login shell.

This is (to my knowledge) only an issue when running linux and when using a non-Conda virtual environment.

Workaround

This can be fixed by making sure that the VS Code root process is started from a shell where the virtual environment is activated.

Example

Default python interpreter is /usr/bin/python3 and the one used by VS Code is ~/venv/bin/python

If numpy is installed in a virtual environment but not the system python, the code:

import numpy as np
print(np.zeros(6))

Will run fine, but tests will cause

FAIL: test.test_hello_world.HelloWorld.test_first
src.hello_world.main does not exist!

If the missing import is instead in tests, the error will be along the lines of:

FAIL: unittest.loader._FailedTest. ...
...
import numpy as np
ModuleNotFoundError: No module named 'numpy'

Desired solution

The optimal fix would be to have tmc test run using the same interpreter as VS Code is using by default.

If this is not possible/practical, the error reporting could be improved to point to the source issue.

@jolampi
Copy link
Contributor

jolampi commented May 22, 2020

Hello,

Thank you for providing details for this issue. We will need to consider how we want to approach it however, as there currently isn't any language-specific behavior in the extension itself. Running python code via the editor is usually achieved with its own extension, while tests are run using an external Java process. For the desired solution we would need to find a reliable way to read this configuration from another extension (which isn't trivial as far as I know) and then patch the test running process in a way that works on all platforms.

Unfortunately this dilemma also applies to error reporting. Without any information about the python environment configuration we necessarily can't distinguish between modules actually missing and python environments differing.

@vondoRishi
Copy link

Description

If using a virtual environment python interpreter for completing exercises, one may run into problems where the interpreter set in VS Code is used for executing the python code, but tests run using the default python interpreter of a login shell.

This is (to my knowledge) only an issue when running linux and when using a non-Conda virtual environment.

Workaround

This can be fixed by making sure that the VS Code root process is started from a shell where the virtual environment is activated.

Example

Default python interpreter is /usr/bin/python3 and the one used by VS Code is ~/venv/bin/python

If numpy is installed in a virtual environment but not the system python, the code:

import numpy as np
print(np.zeros(6))

Will run fine, but tests will cause

FAIL: test.test_hello_world.HelloWorld.test_first
src.hello_world.main does not exist!

If the missing import is instead in tests, the error will be along the lines of:

FAIL: unittest.loader._FailedTest. ...
...
import numpy as np
ModuleNotFoundError: No module named 'numpy'

Desired solution

The optimal fix would be to have tmc test run using the same interpreter as VS Code is using by default.

If this is not possible/practical, the error reporting could be improved to point to the source issue.

I have been able to run the TestMyCode as you have suggested by launching Visual Studio Code from the terminal but now the normal execution ( Run python file in the terminal via green play button) is halted.

@sebazai
Copy link
Contributor

sebazai commented Jul 3, 2020

Ongoing observations

Same problem in any OS.

  • Python interpreter is obtainable from VSCode workspace settings.
  • Implement possibility to pass python executable path ("python.pythonPath") defined in VSCode/Workspace settings to TMC Langs.
  • If python interpreter uses a virtualenv investigate how this would work within TMC Langs.

@sebazai
Copy link
Contributor

sebazai commented Jul 7, 2020

@sebazai sebazai mentioned this issue Jul 23, 2020
7 tasks
@sebazai sebazai mentioned this issue Aug 6, 2020
8 tasks
@sebazai
Copy link
Contributor

sebazai commented Aug 10, 2020

Hello @saskeli

We release version 0.10.0 of the TMC extension in VSCode and would like to hear if this issue has been solved with the new release.
Currently passing the python executable path to TMC-langs is toggled behind the Insider feature toggle, which you can turn on from the Welcome page when updating the extension or by pressing the TMC Icon in the left bar of VSCode, select Settings and toggle the Insider feature from there.

Best regards,
Sebastian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants