Import module from adjacent directory in pytest - python

In test_app, I have tried appending '../../src/app.py' to sys.path, from ...src.app import function or from app import function but I keep getting ModuleNotFoundError: No module named 'app'. Not sure how to overcome this seemingly simple issue.
├── src
│ └── app.py
├── tests
│ └── unit_tests
│ ├── __init__.py
│ ├── conftest.py
│ └── test_app.py

You need to add the full path to the parent folder, then import app:
import sys
sys.path.append("../../src")
# We can now directly access 'app' as we have a pointer to its parent directory
from app import function
# Use 'function' here

Related

Python ModuleNotFoundError: No module named '<directory>''

I want to import a file from subdir and I get an error like that:
ModuleNotFoundError: No module named 'special'
│
├── constants.py
├── crawler.py
├── case.py ===================>>>> I working on this file
├── special
├── __init__.py
└── wantToImport.py =================>>>I want to import this file
My case.py like that:
from special.wantToImport import ImportClass
And my wantToImport.py file like that:
class ImportClass:
def mydefination(self):
#Some codes here
I want to use mydefinitioon function on my case.py file But I cannot import this file.
Why Im getting this error?
How can I solve this?
You should tell Python that your module's path is inside the current directory (using a .):
from .special.wantToImport import ImportClass
Try creating a __init__.py file in the directory where you are working and use it to refer your module.
│
root
├── constants.py
├── crawler.py
├── case.py ===================>>>> I working on this file
├── __init__.py
├── special
├── __init__.py
└── wantToImport.py =================>>>I want to import this file
inport it using
from root.special.wantToImport import ImportClass

how to import a submodule into my test module?

I have a project structured as follows:
.
└── MYAPP/
├── data/ # contains data files i.e: html/json/db files.
├── src/
│ └── myapp/
│ ├── __init__.py
│ ├── core.py
│ └── utils.py
└── tests/
├── conftest.py
├── test_core.py
└── test_utils.py # contains ClassA and ClassB
conftest.py contains the following:
import pytest
from src.myapp.utils import ClassA, ClassB
as you can see in the contents of conftest.py, I am importing the required classes using an absolute import.
By chance while reading through Flask's source code on github, I saw that they were using a similar project structure:
.
└── flask/
├── src/
│ └── flask
└── tests
and in test files were not using absolute imports, for example in the conftest.py file they had the following:
import pytest
from _pytest import monkeypatch
import flask
from flask import Flask as _Flask
My question is, How did flask achieve to import its module into test by just using import flask or from flask import Flask as _Flask. when I attempt to do that (import myapp), I end up with ModuleNotFoundError: No module named 'myapp'

No module named 'app' in absolute path import

I'm working on a flask project which has the following structure:
└──app
├── __init__.py
└── main
├── __init__.py
├── service
│ └── __init__.py (empty file)
│ └── requests_service.py
├─── model
│ └── __init__.py (empty file)
│ └── models.py
└─── util
└── __init__.py (empty file)
└── dto.py
└── requirements.txt
└── .gitignore
IN THE __init__.py INSIDE THE APP FOLDER
from app.main.service.requests_service import RequestsManager
Result:
ModuleNotFoundError: No module named 'app'
In the dto.py inside the util folder
from app.main.model.models import News
Result:
ModuleNotFoundError: No module named 'app'
Basically every module I try to import results in the same error, shown above. I feel like this should be an easy fix, but can't get me head around it. Any help?

Error when import a function from a py file in other directory

I have a directory like
.
└── my_project
├── API
│ ├── __init__.py
│ └── admin.py
└── my_project
├── __init__.py
└── module1_file.py
Inside API/admin.py, I want to import func1 from my_project/module1_file.py. I do like this:
import sys
sys.path.insert(0, './myproject/myproject/')
from module1_file import func1
It throws me this error
from module1_file import func1
ModuleNotFoundError: No module named 'module1_file'
Can anyone please show me how to solve this? Thanks.

import function from a file in the same folder

I'm building a Flask app with Python 3.5 following a tutorial, based on different import rules. By looking for similar questions, I managed to solve an ImportError based on importing from a nested folder by adding the folder to the path, but I keep failing at importing a function from a script in the same folder (already in the path). The folder structure is this:
DoubleDibz
├── app
│ ├── __init__.py
│ ├── api
│ │ ├── __init__.py
│ │ └── helloworld.py
│ ├── app.py
│ ├── common
│ │ ├── __init__.py
│ │ └── constants.py
│ ├── config.py
│ ├── extensions.py
│ ├── static
│ └── templates
└── run.py
In app.py I import a function from config.py by using this code:
import config as Config
but I get this error:
ImportError: No module named 'config'
I don't understand what's the problem, being the two files in the same folder.
Have you tried
import app.config as Config
It did the trick for me.
To import from the same folder you can do:
from .config import function_or_class_in_config_file
or to import the full config with the alias as you asked:
from ..app import config as Config
# imports all functions
import config
# you invoke it this way
config.my_function()
or
# import specific function
from config import my_function
# you invoke it this way
my_function()
If the app.py is invoked not from the same folder you can do this:
# csfp - current_script_folder_path
csfp = os.path.abspath(os.path.dirname(__file__))
if csfp not in sys.path:
sys.path.insert(0, csfp)
# import it and invoke it by one of the ways described above
Another, shorter way would be:
import .config as Config

Categories