0

I have encountered a pattern in a public git repository* hosted by Udacity that I can't find any documentation on:

The folder structure is:

|-- quiz
|     |-- m1 
|          |-- L2
|               |-- quiz_tests.py
|               |-- tests.py
|               |-- quiz notebook.ipynb
|-- tests.py

notebook.ipynb imports import quiz_tests -> which imports tests.py -> which only contains the following: ../../../tests.py (with no quotation marks)

When I try to run this it gives me an import error: cannot import name 'project_test' from 'tests'

What is this pattern and how can I make it work?

Thanks in advance!

*unfortunately the issue asking about the same question got closed without answer

Code:

notebook.ipynb

import quiz_tests

def calculate_returns(close):
    return None

quiz_tests.test_calculate_returns(calculate_returns)

quiz_tests.py

from collections import OrderedDict
import numpy as np
import pandas as pd
from tests1 import project_test, generate_random_tickers, generate_random_dates, assert_output


@project_test
def test_calculate_returns(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'close': pd.DataFrame(
            [
                [21.050810483942833, 17.013843810658827, 10.984503755486879, 11.248093428369392, 12.961712733997235],
                [15.63570258751384, 14.69054309070934, 11.353027688995159, 475.74195118202061, 11.959640427803022],
               ],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'returns',
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan, np.nan, np.nan],
                    [-0.25723988, -0.13655355, 0.03354944, 41.29534136, -0.07731018],
              
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)

tests.py

../../../tests.py
semyd
  • 430
  • 4
  • 17
  • Please add your code snippet. – Ali Irani Dec 02 '21 at 09:15
  • Done, I have added them! – semyd Dec 02 '21 at 09:20
  • You cannot just import a file that has an address to another file. You must use `importlib` library for this purpose. See this question: https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path?rq=1 – Ali Irani Dec 02 '21 at 10:44
  • Does this answer your question? [How to import a module given the full path?](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – Ali Irani Dec 02 '21 at 10:45
  • Thanks for the answer. Do you think this is a mistake in the original github repo? Or do they have a different python runtime configured around it? – semyd Dec 02 '21 at 12:51
  • I think maybe this is a mistake in github repo – Ali Irani Dec 02 '21 at 15:18

0 Answers0