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

Environments needs to be activated before running code on the user's behalf (e.g. unit tests or DS) #3330

Closed
mikofski opened this Issue Nov 13, 2018 · 15 comments

Comments

Projects
None yet
8 participants
@mikofski
Copy link

mikofski commented Nov 13, 2018

Environment data

  • VS Code version: 1.29.0
  • Extension version (available under the Extensions sidebar): 2018.10.1
  • OS and version: Windows10
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.1 (Anaconda) and 3.6.7 (Anaconda)
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda-4.5.11
  • Relevant/affected Python packages and their versions: numpy-1.15.3, pytest-3.10, nose-1.3.7

Related Issues:

Actual behavior

Tests are discovered and run in Python-3.6 conda env, but not in Python-3.7, which doesn't discover tests and/or fails in conftests.py when importing numpy, fails to load DLL multiarray. But numpy does work in a terminal, when py37 is activated, and I can import multiarray from numpy.core.

Also I can even run pytest directly from the terminal inside vscode, so it's something specific to how the tests are implemented. Maybe it's not implementing the conda environment correctly?

Same issue with both pytest and nosetests.

Expected behavior

discover and run tests in Python-3.7 same as in Python-3.6

Steps to reproduce:

  1. install anaconda
  2. create py3.6 and py3.7 environments
  3. install vscode, python extension
  4. open a python project with testing (that uses numpy?), enable testing, select each environment and run tests.

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

here's the output from pytest:

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: c:\Users\mikm\Projects\pvlib-python, inifile:
collected 0 items / 1 errors

=================================== ERRORS ====================================
______________________________ ERROR collecting  ______________________________
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\_pytest\config\__init__.py:429: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('c:\\Users\\mikm\\Projects\\pvlib-python\\pvlib\\test\\conftest.py')

During handling of the above exception, another exception occurred:
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\numpy\core\__init__.py:16: in <module>
    from . import multiarray
E   ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\_pytest\config\__init__.py:435: in _importconftest
    mod = conftestpath.pyimport()
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\py\_path\local.py:668: in pyimport
    __import__(modname)
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\_pytest\assertion\rewrite.py:294: in load_module
    six.exec_(co, mod.__dict__)
pvlib\test\conftest.py:3: in <module>
    import numpy as np
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\numpy\__init__.py:142: in <module>
    from . import add_newdocs
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\numpy\add_newdocs.py:13: in <module>
    from numpy.lib import add_newdoc
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\numpy\lib\__init__.py:8: in <module>
    from .type_check import *
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\numpy\lib\type_check.py:11: in <module>
    import numpy.core.numeric as _nx
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\numpy\core\__init__.py:26: in <module>
    raise ImportError(msg)
E   ImportError: 
E   Importing the multiarray numpy extension module failed.  Most
E   likely you are trying to import a failed build of numpy.
E   If you're working with a numpy git repo, try `git clean -xdf` (removes all
E   files not under version control).  Otherwise reinstall numpy.
E   
E   Original error was: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\py\_path\common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\py\_path\common.py:429: in gen
    for p in self.gen(subdir):
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\py\_path\common.py:418: in gen
    dirs = self.optsort([p for p in entries
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\py\_path\common.py:419: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\_pytest\main.py:599: in _recurse
    ihook = self.gethookproxy(dirpath)
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\_pytest\main.py:417: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\_pytest\config\__init__.py:413: in _getconftestmodules
    mod = self._importconftest(conftestpath)
..\..\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\_pytest\config\__init__.py:452: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: (local('c:\\Users\\mikm\\Projects\\pvlib-python\\pvlib\\test\\conftest.py'), (<class 'ImportError'>, ImportError("\nImporting the multiarray numpy extension module failed.  Most\nlikely you are trying to import a failed build of numpy.\nIf you're working with a numpy git repo, try `git clean -xdf` (removes all\nfiles not under version control).  Otherwise reinstall numpy.\n\nOriginal error was: DLL load failed: The specified module could not be found.\n"), <traceback object at 0x0000012E97C8C688>))
 generated xml file: C:\Users\mikm\AppData\Local\Temp\tmp-18512deYtowEyIRt8.xml 
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.53 seconds ===========================

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

There were some errors in discovering unit tests

t.onDidNotificationChange @ notificationsAlerts.ts:40
(anonymous) @ notificationsAlerts.ts:26
e.fire @ event.ts:145
t.notify @ notifications.ts:113
t.notify @ notificationService.ts:52
e._showMessage @ mainThreadMessageService.ts:83
e._showMessage @ mainThreadMessageService.ts:44
e.$showMessage @ mainThreadMessageService.ts:38
t._doInvokeHandler @ rpcProtocol.ts:439
t._invokeHandler @ rpcProtocol.ts:424
t._receiveRequest @ rpcProtocol.ts:341
t._receiveOneMessage @ rpcProtocol.ts:271
(anonymous) @ rpcProtocol.ts:146
e.fire @ event.ts:145
a @ ipc.net.ts:103
n._socketDataListener @ ipc.net.ts:132
emitOne @ events.js:116
emit @ events.js:211
addChunk @ _stream_readable.js:263
readableAddChunk @ _stream_readable.js:250
Readable.push @ _stream_readable.js:208
onread @ net.js:594
@brettcannon

This comment has been minimized.

Copy link
Member

brettcannon commented Nov 15, 2018

I can discover and run tests fine under conda using Python 3.7 and pytest without numpy, but I do get the same failure if the environment is not activated. It seems conda has chosen to package numpy in such a way that it can't be imported unless the environment is activated (which we don't do when running tests).

With a test of:

import numpy

def test_fake():
    return True

and settings of:

"python.unitTest.pyTestArgs": [
        "."
    ],
    "python.unitTest.unittestEnabled": false,
    "python.unitTest.nosetestsEnabled": false,
    "python.unitTest.pyTestEnabled": true

I end up with:

============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: c:\Users\brcan\Desktop\Testing\Some 3.7 Bunk, inifile:
collected 0 items / 1 errors

=================================== ERRORS ====================================
______________________ ERROR collecting example_test.py _______________________
ImportError while importing test module 'c:\Users\brcan\Desktop\Testing\Some 3.7 Bunk\example_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\core\__init__.py:16: in <module>
    from . import multiarray
E   ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:
example_test.py:1: in <module>
    import numpy
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\__init__.py:142: in <module>
    from . import add_newdocs
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\add_newdocs.py:13: in <module>
    from numpy.lib import add_newdoc
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\lib\__init__.py:8: in <module>
    from .type_check import *
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\lib\type_check.py:11: in <module>
    import numpy.core.numeric as _nx
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\core\__init__.py:26: in <module>
    raise ImportError(msg)
E   ImportError: 
E   Importing the multiarray numpy extension module failed.  Most
E   likely you are trying to import a failed build of numpy.
E   If you're working with a numpy git repo, try `git clean -xdf` (removes all
E   files not under version control).  Otherwise reinstall numpy.
E   
E   Original error was: DLL load failed: The specified module could not be found.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.11 seconds ===========================
__________There was an error in identifying unit tests in example_test.py__________
______________________ ERROR collecting example_test.py _______________________
ImportError while importing test module 'c:\Users\brcan\Desktop\Testing\Some 3.7 Bunk\example_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\core\__init__.py:16: in <module>
    from . import multiarray
E   ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
example_test.py:1: in <module>
    import numpy
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\__init__.py:142: in <module>
    from . import add_newdocs
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\add_newdocs.py:13: in <module>
    from numpy.lib import add_newdoc
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\lib\__init__.py:8: in <module>
    from .type_check import *
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\lib\type_check.py:11: in <module>
    import numpy.core.numeric as _nx
..\..\..\AppData\Local\Continuum\miniconda3\envs\activatetest\lib\site-packages\numpy\core\__init__.py:26: in <module>
    raise ImportError(msg)
E   ImportError: 
E   Importing the multiarray numpy extension module failed.  Most
E   likely you are trying to import a failed build of numpy.
E   If you're working with a numpy git repo, try `git clean -xdf` (removes all
E   files not under version control).  Otherwise reinstall numpy.
E   
E   Original error was: DLL load failed: The specified module could not be found.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
@brettcannon

This comment has been minimized.

Copy link
Member

brettcannon commented Nov 15, 2018

I also forgot to mention that I verified this in an Anaconda Prompt by running directly the Python interpreter and then indirectly through an activated terminal and got the same results, so it's definitely based on how they have chosen to package numpy for at least Python 3.7.

We might have to use child_process.exec() and compose the activation + command together to make this work which is not going to be trivial to do as we would have to re-architect how to execute Python just for this one case.

@mikofski

This comment was marked as resolved.

Copy link
Author

mikofski commented Nov 15, 2018

Seems like a conda bug to me. Why should a Python not work unless activated via conda? What magic does conda do? IMO this is not standard Python. I should be able to use any Python given the correct PYTHONPATH, PYTHONHOME, etc.

Although numpy is a stubborn one, with it's BLAS and LAPACK dependencies. Maybe there's more to this? Do you think I should open a conda issue?

@mikofski

This comment was marked as resolved.

Copy link
Author

mikofski commented Nov 15, 2018

  • macosx-10.13.6: Yes, tests are discovered and run without error in Python-3.7 conda environment with numpy-1.15.4, vscode-1.29.0, python extension v2018.10.1
  • Linux: not sure how to test, would it work to run in WSL?
@mikofski

This comment was marked as resolved.

Copy link
Author

mikofski commented Nov 15, 2018

Should we close this then? I agree this seems like a duplicate of #3040. There's already an issue logged ContinuumIO/anaconda-issues#10257 in anaconda as well

@mingwandroid

This comment was marked as resolved.

Copy link

mingwandroid commented Nov 15, 2018

Seems like a conda bug to me. Why should a Python not work unless activated via conda? What magic does conda do? IMO this is not standard Python. I should be able to use any Python given the correct PYTHONPATH, PYTHONHOME, etc.

This is not a conda bug. The conda package manager allows packages to specify activation and deactivation scripts which, generally speaking must be run for the software to work correctly.

These scripts can do arbitrary things, setting env vars, running programs, whatever they need to.

Simply put, running any Anaconda Distribution software outside of using the correct activation procedure is not and never will be supported. In this particular instance, it's not actually the problem that the activation scripts are not run, it is the more fundamental fact that conda activation sets PATH correctly.

People are free to avoid this advice and in many cases everything will be fine, but it's unsupported (and I have no idea why people think avoiding activation would be considered reasonable, to be clear, if activation was optional it wouldn't exist!).

Finally please remember AD is about more than just Python too.

@mingwandroid

This comment was marked as resolved.

Copy link

mingwandroid commented Nov 15, 2018

This issue should not be closed until it is fixed as per @brettcannon's comments.

@mikofski

This comment was marked as resolved.

Copy link
Author

mikofski commented Nov 15, 2018

This is not a conda bug.

I agree, @mingwandroid, and I'm sorry. I typed too hastily, and I should've thought about it more carefully.

Finally please remember AD is about more than just Python too.

I started to realize this as I was typing, but I should have revised my comments before submitting.

Although numpy is a stubborn one, with it's BLAS and LAPACK dependencies. Maybe there's more to this?

@gvellut

This comment has been minimized.

Copy link

gvellut commented Nov 21, 2018

I had the same issue. Until it is fixed, I created a batch file :

@echo off
C:\\Users\\gvellut\\Anaconda3\\Scripts\\activate.bat wbt2 && pytest %*

And configured the "python.unitTest.pyTestPath" option to launch it (instead of pytest directly).

@brettcannon

This comment has been minimized.

Copy link
Member

brettcannon commented Nov 21, 2018

Just so people know, we're trying to schedule a meeting with Anaconda to figure out how best to handle this. Once we have that meeting we will be in a better place to know how to solve this going forward.

@brettcannon brettcannon changed the title test frameworks fails with python 3.7 from anaconda when using a conda environment, the environment is not activated when running tests Nov 21, 2018

@brettcannon brettcannon added the P1 label Jan 2, 2019

@qubitron qubitron changed the title when using a conda environment, the environment is not activated when running tests Conda needs to be activated starting with Python 3.7 Jan 2, 2019

@qubitron

This comment has been minimized.

Copy link

qubitron commented Jan 2, 2019

We'll need to check the performance before committing this change.

@qubitron qubitron changed the title Conda needs to be activated starting with Python 3.7 Conda needs to be activated before running code on the user's behalf Jan 2, 2019

@DonJayamanne DonJayamanne self-assigned this Jan 7, 2019

@DonJayamanne

This comment has been minimized.

Copy link

DonJayamanne commented Jan 7, 2019

Findings, running jupyter in pipenv also requires the environment to be activated.
I.e. activation of environments is not limited just to Conda, we'll need to do this for other environments too.

Proposed Solution:

  • Create a method namd createActivatedEnvironment in IPythonExecutionFactory
  • This new method will asynchronously return an implementation of IPythonExecutionService, which will in turn ensure the python environment is activated before executing any python code.
@brettcannon

This comment has been minimized.

Copy link
Member

brettcannon commented Jan 7, 2019

@DonJayamanne How does the failure manifest itself? I would be curious to know why it's occurring (if it's because of virtualenv then I can understand that).

@DonJayamanne DonJayamanne changed the title Conda needs to be activated before running code on the user's behalf Environments needs to be activated before running code on the user's behalf (e.g. unit tests or DS) Jan 10, 2019

@ericsnowcurrently

This comment has been minimized.

Copy link
Member

ericsnowcurrently commented Jan 29, 2019

I validated this.

@mikofski

This comment has been minimized.

Copy link
Author

mikofski commented Jan 29, 2019

yay! thanks so much everyone!

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