0

I have a python file which contains classes and functions and a

if __name__ == "__main__":
    ...

construct. I want to test a class which is defined in this file with py.test. For my first "hello world"-test, my test-file only imports the file to be tested.

Running py.test in PyCharm results however in

raceback (most recent call last):
  File "[...]/unittest/datageneration_test.py", line 1, in <module>
    from main.datageneration import *
  File "[...]/main/datageneration.py", line 1, in <module>
    import pandas as pd
  File "[...]/lib/python3.6/site-packages/pandas/__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']

My file to be tested has the import statements

import pandas as pd
import numpy as np
from scipy.special import expit
import sys
import os
import scipy.io.wavfile as wav
import json
import uuid

My conda environment.yml is

channels:
- conda-forge
- defaults
dependencies:
- pandas=0.20.3
- numpy=1.13.1
- ggplot=0.11.5
- python=3.6.2
- pytest=3.2.1

What is the issue here?

Make42
  • 12,236
  • 24
  • 79
  • 155
  • Hope it will help https://stackoverflow.com/questions/42973666/importerror-missing-required-dependencies-numpy – Anup Aug 16 '17 at 08:47
  • If you are running from a conda environment, you need to point your pycharm to use that environment. – BoboDarph Aug 16 '17 at 08:49
  • @Anup: The link suggests I delete ".pyc" files, but I don't see any in my folders. – Make42 Aug 16 '17 at 08:50
  • @BoboDarph: My PyCharm is pointing towards that environment - both the general setting is such and the setting for that specific test. – Make42 Aug 16 '17 at 08:51
  • Can you try explicitly uninstalling and re-installing numpy and pandas in your conda env? Just open it in an anaconda prompt and do pip uninstall pandas pip uninstall numpy pip install pandas pip install numpy. Taken from here: https://stackoverflow.com/a/42939561/8085234 – BoboDarph Aug 16 '17 at 08:53
  • @BoboDarph: I reinstalled the entire conda environment from scratch, but this did not help. I added the versions of the package I am using in the question. Maybe it is a version issue? (Welcome in Python dependency hell?) – Make42 Aug 16 '17 at 09:11
  • Probably numpy version doesnt match what pandas needs. Or numpy is being a tease and not exposing itself properly. Taken from another unfortunate soul: https://github.com/anthony-tuininga/cx_Freeze/issues/205#issuecomment-318129677. Fix there was adding numpy.core._methods instead of numpy to the dependencies. – BoboDarph Aug 16 '17 at 09:19
  • @BoboDarph: Until I started with py.test, pandas and numpy played nice together. – Make42 Aug 16 '17 at 09:24
  • https://github.com/conda/conda/issues/2075 – BoboDarph Aug 16 '17 at 10:20

1 Answers1

0

I am not quite sure what helped, but I reinstalled/updated numpy and pandas with conda install .... I was still left with an error about having wrong file names. After trying out a lot of stuff I found out that - even though I was using pytest, not the library unittest - I could not name the folder in which all my test files are "unittest". Renaming it to "unittests" solved that. Now everything works.

Make42
  • 12,236
  • 24
  • 79
  • 155
  • It's usually a good idea to avoid using the same names as [standard library modules](https://docs.python.org/3/py-modindex.html), because modules depending on the stdlib will end up importing your module. If the module can't be renamed, the import issue can be avoided by moving the module underneath a package (a directory with \_\_init\_\_.py in it). – theY4Kman Aug 18 '17 at 19:04