Wincenty Bertoni Lech
Wincenty Bertoni Lech

Reputation: 495

Trying to import a class from another file but my test breaks

I'm trying to use prettyconf - https://github.com/osantana/prettyconf - to use a .env file.

I created a config.py file and put it in the same folder of my script.

My config.py is this one:

from prettyconf import config


class Settings:
    ENVIRONMENT = config(
        'ENVIRONMENT',
        default='dev',
        cast=config.option({'dev': 'dev', 'int': 'int', 'prod': 'prod'}),
    )

    LOG_LEVEL = config('LOG_LEVEL', default='INFO')
 

settings = Settings()

In my script I import my config.py in this way:

from cl_uploader.config import settings

But I got this error msg:

Traceback (most recent call last):
  File "cl_uploader.py", line 7, in <module>
    from cl_uploader.config import settings
  File "/home/myfolder/Doing/folder/cl_uploader/cl_uploader.py", line 7, in <module>
    from cl_uploader.config import settings
ModuleNotFoundError: No module named 'cl_uploader.config'; 'cl_uploader' is not a package

I tried to change to a relative path like this:

from .config import settings

But I got this error:

Traceback (most recent call last):
  File "cl_uploader.py", line 7, in <module>
    from .config import settings
ImportError: attempted relative import with no known parent package

But if a let like this:

from config import settings

It works! But...

My tests starts to break and I get this msg:

____________________________ ERROR collecting tests/test_cl_uploader.py _____________________________
ImportError while importing test module '/home/myfolder/Doing/folder/tests/test_cl_uploader.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../.cache/pypoetry/virtualenvs/cl-uploader-12nYBdPj-py3.8/lib/python3.8/site-packages/_pytest/python.py:511: in _importtestmodule
    mod = self.fspath.pyimport(ensuresyspath=importmode)
../../.cache/pypoetry/virtualenvs/cl-uploader-12nYBdPj-py3.8/lib/python3.8/site-packages/py/_path/local.py:704: in pyimport
    __import__(modname)
../../.cache/pypoetry/virtualenvs/cl-uploader-12nYBdPj-py3.8/lib/python3.8/site-packages/_pytest/assertion/rewrite.py:152: in exec_module
    exec(co, module.__dict__)
tests/test_cl_uploader.py:10: in <module>
    from cl_uploader.cl_uploader import check_stack_exists
cl_uploader/cl_uploader.py:7: in <module>
    from config import settings
E   ModuleNotFoundError: No module named 'config'
====================================== short test summary info ======================================
ERROR tests/test_cl_uploader.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================= 1 error in 0.25s ==========================================

My tree folder is set this way:

.
├── cl_uploader
│   ├── cl_uploader.py
│   ├── config.py
│   ├── __init__.py
│   └── resources
│       └── teste.yaml
├── file.tmp
├── local.env
├── Makefile
├── poetry.lock
├── pyproject.toml
├── README.md
└── tests
    ├── __init__.py
    └── test_cl_uploader.py

Upvotes: 1

Views: 1881

Answers (2)

finswimmer
finswimmer

Reputation: 15212

Your package is called cl_uploader and you have a module cl_uploader within this package. Avoid this duplication of names.

By default python will look in the current working directory first if it can find the module or package, before going through other places.

So when you are inside the cl_uploader folder, it will pickup the cl_uploader module instead of the package. If you are outside it will pickup the package.

In summary: rename cl_uploader.py to something else and everything will work fine.

Upvotes: 0

GAEfan
GAEfan

Reputation: 11370

You are already in the cl_uploader folder. Try: from config import settings.

In __init__.py add this:

import os, sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)))

Then, all files in this directory will be on sys.path. Might want to change the filename to cl_config.py to avoid conflicts.

Upvotes: 2

Related Questions