If my project is structured like this:
I have the following folder structure.
project
├── main
│ └── daily.py
└── utils
└── config.py
How can I import utils.config from main/daily.py when I run "python project\main\daily.py"?
Related
I have a problem with the import file in the bash script.
My project directory looks like this:
project
├── folder1
│ └── file1.py
├── folder2
│ └── file2.py
├── data
│ └── example.txt
├── utils
│ ├── __init__.py
│ └── globals.py
└── run.sh
The globals.py file contains some config global variables that will be used in the whole project.
I want to write a bash file to run all .py files like this run.sh:
conda activate pyenv
# Step 1.1
python folder1/file1.py data/example.txt >> data/output_1_1.txt
# Step 1.2
python folder2/file2.py data/output_1_1.txt >> data/output_1_2.txt
But then I got this error:
Traceback (most recent call last):
File "folder2/file2.py", line 15, in <module>
from utils import globals
ModuleNotFoundError: No module named 'utils'
This is my file2.py where I import utils.globals:
import sys
sys.path.append("../") # Add "../" to utils folder path
from utils import globals
When I run each file individually, it works fine, but I don't know why it doesn't work when I run source run.sh.
As this answer points out it is not good to use a relative path when the project structure is complex.
So I change my project directory to this to make it a package like #bigbounty's suggested:
project
├── package
│ ├── folder1
│ │ └── file1.py
│ ├── folder2
│ │ └── file2.py
│ ├── data
│ │ └── example.txt
│ └── utils
│ ├── __init__.py
│ └── globals.py
└── run.sh
In the file2.py, change the import to this:
from package.utils import globals
The run.sh file:
conda activate pyenv
# Step 1.1
python -m package.folder1.file1 package/data/example.txt >> package/data/output_1_1.txt
# Step 1.2
python -m package.folder2.file2 package/data/output_1_1.txt >> package/data/output_1_2.txt
To run the bash script, cd to the /project:
(base) user#user1:~/project$ source run.sh
Then it works as expected.
I have the following project structure:
python_project
├── module
│ ├── constants.py
│ └── __init__.py
├── scripts
│ ├── __init__.py
│ └── script.py
└── tests
├── constants.py
└── __init__.py
python_project is in PYTHONPATH.
module/constants.py:
VAR_MODULE = 25
tests/constants.py:
VAR = 17
I'm facing the following issue in script.py file:
from module.constants import VAR_MODULE
works
from tests.constants import VAR
throws the following exception:
from tests.constants import VAR
ImportError: cannot import name 'VAR'
I know that there is no point in importing stuff from tests directory, just wondering why this does not work. Is tests directory excluded somehow?
Thanks!
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'
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?
app structure:
.
├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.md
├── template.yaml
├── tests
│ ├── __init__.py
│ └── unit
│ └── lambda_application
│ ├── test_handler.py
│ └── test_parent_child_class.py
└── lambda_application
├── __init__.py
├── first_child_class.py
├── lambda_function.py
├── second_child_class.py
├── requirements.txt
└── parent_class.py
4 directories, 14 files
Code sample from lambda_function.py:
import os
import json
from hashlib import sha256
import boto3
from requests import Session
from .first_child_class import FirstChildClass
def lambda_handler(event, context):
# Do some stuff.
As is, I get the error message
Unable to import module 'lambda_function'
but If I comment out the last import, from .first_child_class import FirstChildClass, it is able to get past that part and get the error that I haven't loaded the module for that class.
I only seem to get this error when I run it in the lambci/lambda:python3.7 docker image and when I deploy on AWS. All my tests pass and it is able to import the module with no problems.
Is there something I should load/setup in the __init__.py file?
EDIT I changed the names of some of the files to post it here.
You are using a relative import here which works in case the code you are executing is in a module. However, since your code is being executed not as a module, your AWS Lambda fails.
https://stackoverflow.com/a/73149/6391078
A quick run locally gave the following error:
PYTHON 3.6
Traceback (most recent call last):
File "lambda_function.py", line 4, in <module>
from .first_child_class import FirstChildClass
ModuleNotFoundError: No module named '__main__.first_child_class'; '__main__' is not a package
Your tests pass because your testing suite imports the file as a module from the lambda_application folder which gets treated as a package in the testing module
This got me going in the correct direction but didn't quite give me the answer but did lead me to the answer, so I thought I would update what I found here.
I didn't try it but from what I found, I believe that:
from first_child_class import FirstChildClass
would be the simplest resolution.
What I ended up doing was moving the classes into a sub-directory and essentially did the same as above but with a package name prepended.
So, the file structure changed to:
.
├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.md
├── template.yaml
├── tests
│ ├── __init__.py
│ └── unit
│ └── lambda_application
│ ├── test_handler.py
│ └── test_parent_child_class.py
└── lambda_application
├── __init__.py
└── lib
├── first_child_class.py
├── second_child_class.py
└── parent_class.py
├── lambda_function.py
└── requirements.txt
and my import became from lib.first_child_class import FirstChildClass