NameError (wrong entry of Import) and ImportError (no _init_.py)

flagchecker flowchart:

***/
  ├ flagchecker/
  │    ├ flagchecker/
  │    │    ├ __init__.py 
  │    │    ├ main.py
  │    │    └ dict.json
  │    └ test/
  │        └ test_main.py
  ├ pipfile
  └ pipfile.lock

test_main.py:

import json
from flagchecker.flagchecker import main

def test_get_base_date():
    myurl = "C:/Users/***/flagchecker/flagchecker/dict.json"
    dict_json = json.load(open(myurl,"r"))
    dict_json["config"]["candle_bar"] = "1day"
    base_date, pre_date = BaseDate(dict_json).get_base_date()
    assert base_date == "2021" and pre_date == "2020"

When I run pytest, an error occurs ... NameError:’BaseDate'is not defined The class "BaseData" in main.py cannot be found. It seems that The details of the error are as follows

======================= test session starts ========================
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: C:\Users\***\flagchecker\test
collected 1 item

test_main.py F                                                [100%]

============================= FAILURES ============================= 
________________________ test_get_base_date ________________________

def test_get_base_date():
    myurl = "C:/Users/***/flagchecker/flagchecker/dict.json"   
    dict_json = json.load(open(myurl,"r"))
    dict_json["config"]["candle_bar"] = "1day"
    base_date, pre_date = BaseDate(dict_json).get_base_date()    

NameError: name'BaseDate' is not defined ← Here

test_main.py:8: NameError
===================== short test summary info ====================== 
FAILED test_main.py::test_get_base_date - NameError: name 'BaseDat...
======================== 1 failed in 2.83s ========================= 

It seems that I could not import it when I tried various things Modified as follows

import json

from flagchecker import main ← here

def test_get_base_date():
    myurl = "C:/Users/***/flagchecker/flagchecker/dict.json"
    dict_json = json.load(open(myurl,"r"))
    dict_json["config"]["candle_bar"] = "1day"
    base_date, pre_date = main.BaseDate(dict_json).get_base_date()
    assert base_date == "2021" and pre_date == "2020"

With this, the NameError has been resolved, This time, ImportError occurs ...

======================= test session starts ========================
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: C:\Users\***\flagchecker\test
collected 0 items / 1 error

============================== ERRORS ==============================
__________________ ERROR collecting test_main.py ___________________
ImportError while importing test module 
'C:\Users\***\flagchecker\test\test_main.py'.
Hint: make sure your test modules/packages have valid Python names.  
Traceback:
..\..\appdata\local\programs\python\python38\lib\importlib\__init__.py:127: in 
import_module
    return _bootstrap._gcd_import(name[level:], package, level)      
test_main.py:2: in <module>
    from flagchecker import main
E   ImportError: cannot import name 'main' from 'flagchecker' 
(c:\users\***\appdata\local\programs\python\python38\lib\site- 
packages\flagchecker\__init__.py)
===================== short test summary info ====================== 
ERROR test_main.py
!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!! 
========================= 1 error in 0.43s =========================

Apparently there is init.py! Because it seems like that Init.py was added to the test folder as follows. flagchecker flowchart:

***/
  ├ flagchecker/
  │    ├ flagchecker/
  │    │    ├ __init__.py 
  │    │    ├ main.py
  │    │    └ dict.json
  │    └ test/

│ ├ __init __.py ← here │ └ test_main.py ├ pipfile └ pipfile.lock Then run pytest again!

======================== test session starts ======================== 
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: C:\Users\***\flagchecker\test
collected 1 item                                                      

test_main.py .                                                 [100%] 

========================= 1 passed in 0.09s ========================= 

The pytest has been completed successfully.

Recommended Posts

NameError (wrong entry of Import) and ImportError (no _init_.py)
Import of japandas with pandas 1.0 and above
python> import seiral> ImportError: No module named serial