Unable to import module when developing a python package - python

My project structure is:
APP(dir)
app(dir)
model(dir)
metric_data.py
__init__.py
app.py
setup.py
When I'm running app.py with import statement of
from model.metric_data import MetricData
MetricData is a class, I'm able to successfully run the application retrieving data from metric_data.py file. But when I build it as a package and then try importing package app
from model.metric_data import MetricData
this statement is failing. Can anyone help me with the issue
here, I looked on the relative import part and tried but it didn't work.

Try to create a __init__.py file in the model directory.

Related

How to import app file into test file in different directory Python

apologies if I'm going about this wrong, but I am quite new to python and can't quite figure out what the problem is! I have a simple Flask app that I'm trying to write tests using pytest for. The file structure is like this:
│── app.py
|── tests
│ ├── tests_app.py
I originally had app.py and tests_app.py in the same level and it all worked fine with tests passing, but now I have put tests in their own folder I can no longer import app without error.
I have tried the following in tests_app.py:
from app import app #this is what worked fine when app and tests were in the same folder
from ..app import app
from .. import app
and the linting error is 'Attempted relative import beyond top-level package' and when I run pytest it says 'ImportError: attempted relative import with no known parent package'.
Many thanks in advance!
p.s. I am using Python 3.8.5
You need to have app on the module search path. A quick fix would be to add the path to app.py to the PYTHONPATH environment variable:
export PYTHONPATH=/path/to/dir # where app.py is
If app.py is on the python path, then import app will work (barring any other import problems such as circular imports).
An effective and reproducible way to do this is to install your application. You can do this in develop mode, which means the code runs from its current directory. This is an understated, important part of developing a Python application, but to do it you will need to write code to package your application.

Cannot import package from a different folder

I am working on a project based on Python 2.7 and I am trying to import a module which is in a package folder that contains __init__.py and the file that I want to import called package1.py, but I am unable to do so. This is my folder structure: main_project/Tools/common/package1.py
Note that my project files are in the folder main_project. So, I am trying to call the package1.py by using an import statement in my script:
from Tools.common.package1 import variable
But I am getting an ImportError: No module named Tools.common.package1.
What is the solution to solving this error as I want to use the package feature for my project?
Maybe use the solution i found here :
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
or verify your module has a __init__.py
Importing files from different folder
Ok I have found the answer. I also had to insert an init.py in the folder Tools as well. Initially I only inserted init.py in common but not in Tools as we should also make Tools a package if we want to access common

ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package

I have a Django project with the following structure:
project
apps
news
__init__.py
models.py
hose
tasks.py
Within tasks.py, I have this import
from apps.news.models import Company
When I try to run tasks.py, I get the following error:
ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package
How do I fix this error? I don't understand why this import does not work.
Reference: https://docs.python.org/2/tutorial/modules.html#packages
You need to add an empty __init__.py (4 underscores in total) file in the apps folder for it to be recognized by Python as a package.
In case anybody else is still struggling with this. I had the __init__.py in the app folder but it still didn't work. Running the following command in app parent folder worked for me.
export PYTHONPATH=$PWD

How to run script from parent directory in Flask?

I'm working on a Flask app and run into issues with trying to run a script within a module where the script is in a different directory. I've tried looking at several solutions here and on other sites and haven't been able to find something that works. I have a project structure like so:
dashboard\
app\
static\
templates\
__init__.py
jobs.py
api_fetch.py
config.py
run.py
Within jobs.py I have a function that needs to run api_fetch.py but for the life of me, I'm not sure what I need to do to that. I've tried imports with .., sys, os and nothings worked. This seems like it shouldn't be that difficult, but I'm at a loss. So far I've only needed to import modules on the same path which work fine.
in jobs.py:
from .. import api_fetch
then 'outside' dashboard:
$ python -m dashboard.app.jobs
__init__.py should be in dashboard also.
You can just import from parent package dashboard, the file structure should like this:
dashboard\
|app\
|static\
|templates\
-__init__.py
-jobs.py
-__init__.py << don't forget this!
-api_fetch.py
-config.py
-run.py
Then in jobs.py:
from dashboard import api_fench

PyInstaller and Python modules

I'm currently trying to bundle my Python (3.4.4) application with PyInstaller. I'm following the PyInstaller documentation about how to build a .spec file.
This is how my project file tree looks like:
project/
__init__.py
main.py
ui.py
Lib/
__init__.py
History.py
Command.py
Graphics.py
Tools.py
According to the documentation, all I have to do is to run the pyi-makespec main.py function and the Lib module will be detected as long as it is imported from the main.py file.
Documentation:
Because your script includes the statement import helpmod, PyInstaller will create this folder arrangement in your bundled app
https://pythonhosted.org/PyInstaller/spec-files.html#using-data-files-from-a-module
This is how the beginning of the my main.py looks like
# main.py
from Lib.Command import Command
from Lib.History import History
from Lib.Graphics import Graphics
import Lib.Tools as Tools
When I try to run the app afterwards, all I get is this error. I probably missed something, does someone have an idea of the problem ? :D
error log : http://pastebin.com/3dygTqfn
EDIT : Just figured out that the problem comes from my Graphics.py library, which import some bokeh tools I use to generate histograms. Still don't know why the bokeh import makes the whole thing crash, it works perfectly fine when I run it with a python interpreter...

Categories