ImportError - Module Not Found? - python

My directory structure is:
project_folder/
..my_project/
....server/
......server.py
......api.py
When I'm in the project_folder and run python3 my_project/server/server.py, I get ModuleNotFoundError: No module named 'my_project' with the line of code:
from my_project.server.api import app as application
Weirdly, when I run my tests with Pytest everything passes. I've been trying to solve my problem without using this code snippet, which I always see recommended to solve these problems:
import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
Any tips?

The module you try to call is already in the same package, so just do
from api import app as application

Add an empty __init__.py file in my_project and server folders.

run this cd my_project/server/ and then python server.py
Or remove this line from server.py import my_project
And if you are trying to make a module add __init__.py to your server dir

Related

Cant import from file in python

I am running a Flask server in Docker and cannot import create_app from app.py into my integration tests. I've tried a variety of naming approaches but Python is unable to find app.py.
The directory structure is as follows
/server
/test/integration/test.py
app.py
test.py has this import
from app import create_app
I tried relative imports as well but there was a "parent" error. I then played around with empty __init__.py files in an attempt to use relative imports. That didn't work. I am not sure why this is so involved to do really. What is the solution for this?
In test.py add:
# some_file.py
import sys
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/app/folder')
import app
from app import create_app
This add the directory of the source file to the system so Python will know to go there when searching the things to import. This is true when both files are local on your computer. If there is a server you need to see how you modify the in-server Python environment to know the folder of the app.py file.
See: Importing files from different folder
You can try with relative imports
from ..app import create_app

ModuleNotFoundError: No module named in another submodule

I have read a couple of threads on StackOverflow but did not find any answer that has fixed my problem. I'm pretty new to Python and cannot figure out how does modeling system works. I do understand the difference between module and package, but that does not help me with my problem. Here goes:
Here's my project structure:
venv/
root/
commons/
config.py
main/
main.py
Here's my config.py:
class Config:
...
Here's my main.py
from commons.config import Config
...
when running (venv) python3 root/main/main.py I get ModuleNotFoundError: No module named 'config'. What am I doing wrong? It is a problem with my app? with my main.py? With the way I execute my main.py?
Execution is done using Python 3.9 on MacOS
The path to config.py is not added to the list of paths where the interpreter looks for modules and hence does find the config module.
A simple workaround is to change in main.py:
from commons.config import Config
to
from root.commons.config import Config
and execute main.py as a module in the project directory with
python -m root.main.main
When the file is executed as a module it will add the path from where it is executed to the paths the interpreter looks for modules and root.commons.config is a absolute reference from then on.
As #Iliya mentioned, the interpreter doesn't find the way to 'config' module.
You can modify your main.py to fix the issue:
import sys
sys.path.append("absolute_path_to_root")
from commons.config import Config
...
or
import os
import sys
# set the parent dir into python's search path
current = os.path.dirname(os.path.realpath(__file__))
parent = os.path.dirname(current)
sys.path.append(parent)
from commons.config import Config
...
After carefully browsing StackOverflow I finally managed to find a solution that works for me using setup.py.
Thank you #np8 for this answer

Gitlab CI Python run test - ModuleNotFoundError: No module named xxx

I saw a lot of questions about importing modules error but I could not manage to solve the problem with Gitlab CI pipeline.
My project structure:
├───config
├───docs
├───src
__init__.py
│ ├───dataset
__init__.py
│ ├───exceptions
│ ├───resources
│ └───utils
__init__.py
└───tests
__init__.py
└───resources
__init__.py
I would like to run tests using pytest.
I invoke this command python -m pytest -p no:cacheprovider or using unittest
'python -m unittest discover -v' from root directory and also tried to invoke from test directory. In both cases I have a problem with importing class from dataset module. What's interesting, I have two tests file.
First file imports:
import os import unittest
from src.utils.FileManager import FileManager
Second imports:
from src.dataset.DatasetHelper import DatasetHelper
The first file is passing but the second is failing with error:
from dataset import DatasetHelper ModuleNotFoundError: No module
named 'dataset'
So the thing is that other modules like utils from src are imported correctly, only the dataset has a problem. I am struggling with this a few days and I completely out of ideas. I also tried to change instead of from dataset to from src.dataset. It didn't work. I can run tests in PyCharm by clicking right mous button and just run tests but not on CI environment.
What I tried:
Add modules to $PYTHONPATH like
sys.path.insert(0, "/builds/USER/PROJECT/src/dataset")
sys.path.insert(0, "/builds/USER/PROJECT/src")
sys.path.insert(0, "/builds//USER/PROJECT/tests")
The content of PYTHONPATH before adding it was:
Current $PYTHONPATH: ['/builds/USER/PROJECT/config', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
The first module in list is config because I run script from this directory to add above modules to path. Doesn't Help
Run test command from root directory and add prefix src to imports in tests directory. Doesn't Help
from dataset import DatasetHelper
ModuleNotFoundError: No module named 'dataset'
Either in src.__init__ or more proabably in src.dataset.__init__ there is the import statement from dataset import DatasetHelper. You have to rewrite it as from src.dataset import…
You can try using a relative import in your __init__.py file that's inside the tests directory.
The syntax depends on the current location as well as the current location of the module,package, or object you're trying to import. Here are some examples:
from .some_module import some_class
from ..some_package import some_function
from . import some_class
Source: https://realpython.com/absolute-vs-relative-python-imports/

Unable to import module from parent directory in package

I have the following the directory structure
pytest_testing/
__init__.py
math_ops.py
dbs/
__init__.py
dbConnect.py
tests/
test1.py
Now in test1.py i am trying to do import a function from dbConnect module, like this from pytest_testing.dbs.dbConnect import query_data but i get error "No Module named pytest_testin.
If i try the same in a directory above pytest_testing everything works just fine. Am i missing something here ?
Well, you can just type this from dbs.dbConnect import query_data.
I think this can work well
If you are using Linux or Windows use the code:
# test1.py
import sys
sys.path.append(/path/to/pytest_testing/dbs/)
from dbConnect import functionName #dbConnect without .py extension

Python package missing when deploying

On my development (Win7) machine, my app runs fine. The folder structure looks like this (the script being run is run.py below):
package1/
__init__.py
run.py
..
Inside the app, there are some modules which do stuff like
import app from package1
Which works fine.
However, when I try deploying to a linux machine, and run that exact same file, I get an error:
ImportError: No module named package1
I looked into the sys.path of both machines when this script is being run. The first two lines of the windows machine are these:
C:\\Users\\USERNAME\\IdeaProjects\\PROJ_NAME\\package1
C:\\Users\\USERNAME\\IdeaProjects\\PROJ_NAME
Whereas the linux one only has this:
/home/username/webapps/PROJ_NAME/package1
I recognize the problem is that the second line is missing. But why is it missing? What am I missing? Did I build the folder structure wrong?
This is a crappy solution, and I don't like it, but it works.
I added this to run.py
# Fixing the python path
import sys
import os
file_location = os.path.dirname(os.path.abspath(__file__))
project_dir = os.path.abspath(os.path.join(file_location, os.pardir))
if not project_dir in sys.path:
sys.path.insert(0, project_dir)

Categories