nipraxis-fall-2022 / diagnostics-NME

0 stars 4 forks source link

unable to find detectors.py when running test_detectors.py #17

Closed Nauman702 closed 2 years ago

Nauman702 commented 2 years ago

@effigies Hi, it looks like there is another issue. When I run python -m pytest .\findoutlie\tests\test_detectors.py, it gives me an error, unable to find detectors.py module. I have to manually put detectors.py module in the test folder to make it work. Is there something wrong or I am not on the right track?

Thank you

Nauman702 commented 2 years ago

Or I have to do from findoutlie.detectors import iqr_detector

effigies commented 2 years ago

Or I have to do from findoutlie.detectors import iqr_detector

The comments in the file suggest to modify sys.path, but the more resilient thing is either to use from findoutlie.detectors import iqr_detector (will work right now) or from ..detectors import iqr_detector (which will require turning your test directory into a directory module by adding an __init__.py).

I would at least recommend looking at the solution and understanding why it works, but then you can go ahead and do what you're proposing.


The reason the comments suggest the more brittle approach is that this is intended to walk you through a progression that most new Python programmers go through, which is trying to figure out how to get their modules loaded and make progress on their code before constructing them into a proper package. Modifying sys.path will get the job done, while configuring a package correctly can take time and frustration. We gave you a pre-configured package to prevent it from blocking you from doing the project, but that can make things appear misleadingly simple.

Nauman702 commented 2 years ago

@effigies Thank you Chris for the explanation. I found out if I use from findoutlie.detectrors import iqr_detection then i can only test it with pytest. It still doesn't find the module if I just run test_detectors.py. Is it correct way of adding path to the python sys.path import sys sys.path.insert(0, "/diagnostics-NME/findoutlie"), its not working though.

Thank you

Nauman702 commented 2 years ago

@effigies Thats fine. I found out. sys.path.append worked and both test runs work. Thank you again.

effigies commented 2 years ago

Right, we need to get findoutlie onto your path. If your absolute path is /diagnostics-NME/findoutlie, then sys.path.insert(0, "/diagnostics-NME") would allow it to be found.

I would recommend instead using pip install -e . in your repository directory, which will install your package in "editable mode".

Click for more detail When you run `python -m pytest`, pytest (or possibly python...) will add the current directory into your path, and `findoutlie` is a module in your current directory. Calling `pytest` on its own will not, and neither will `python findoutlie/tests/test_detectors.py`. Because you do have a package, you can use `pip install` to put the package into your path. `pip install .` would make a copy of the Python modules into a directory called `site-packages`, so I see something like: ``` /home/chris/miniconda3/envs/diagnostics/lib/python3.10/site-packages/findoutlie ├── detectors.py ├── __init__.py ├── metrics.py ├── outfind.py ├── __pycache__ │   ├── detectors.cpython-310.pyc │   ├── __init__.cpython-310.pyc │   ├── metrics.cpython-310.pyc │   ├── outfind.cpython-310.pyc │   └── spm_funcs.cpython-310.pyc ├── spm_funcs.py └── tests ├── ds107_sub012_t1r2_small.nii ├── get_global_signals.m ├── global_signals.txt ├── __pycache__ │   ├── test_detectors.cpython-310.pyc │   ├── test_dvars.cpython-310.pyc │   └── test_spm_funcs.cpython-310.pyc ├── test_detectors.py ├── test_dvars.py └── test_spm_funcs.py ``` Python will look into its `site-packages` directory to find modules, so this module will be accessible by calling `import findoutlie` from anywhere. Note, however, that these are all copies of what was in your repository. If you make changes, you need to reinstall. Another thing you can do is `pip install -e .`, and instead you see: ``` /home/chris/miniconda3/envs/diagnostics/lib/python3.10/site-packages/findoutlie-0.1a0.dist-info/ ├── direct_url.json ├── INSTALLER ├── METADATA ├── RECORD ├── REQUESTED └── WHEEL ``` The `direct_url.json` contains information that tells Python where to look for the module: ```JSON { "dir_info": { "editable": true }, "url": "file:///home/chris/Projects/nipraxis/nipraxis-fall-2022/diagnostics-NME" } ``` So any changes you make will get picked up next time you run Python.
jagruti8 commented 2 years ago

@Nauman702 hey, before proceeding for the tests it was asked to install the "findoutlie" directory as mentioned here : https://apps.learn.nipraxis.org/learning/course/course-v1:Nipraxis+NI1+Fall2022/block-v1:Nipraxis+NI1+Fall2022+type@sequential+block@5ba09e862c8647ed8f4b56082f9baff7/block-v1:Nipraxis+NI1+Fall2022+type@vertical+block@1f168b5888ce4d5c86f38e99eb9eab63 I guess, you might have missed this. Actually I too made the same mistake.