How to run script from parent directory in Flask? - python

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

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.

Importing files in different directories in Python

I have a directory structure like this:
dir/
frontend.py
dir1/main.py
dir2/backend.py
How do I import backend in main in Python?
How do I import frontend in main in Python?
Have tried all the answers on Stackoverflow. Nothing seems to work.
In any folder from which you want to import source files you need to have existing init.py file.
I would advise structure like:
dir/
main.py
dir1/frontend.py
dir1/__init__.py
dir2/backend.py
dir2/__init__.py
Then you import them in following fashion (in main.py):
import dir1.frontend
import dir2.backend
There is only one rule when it comes to importing files in a Python project.
You have to import the package relative to the directory from where the project is run.
For example in the question main.py should have something like this:
from dir.frontend import *
from dir.dir2.backend import *
But then you'll have to have something like main.py under dir/ which imports dir/dir1/main.py and then run python main.py.
So, try to keep the main.py always in the head directory so you don't have to worry about such a situation as above.
ONLY ONE RULE: Everything has to be imported relatively to the directory from where the project is run.

Python import system mechanics for split test and app directories

I am struggling to successfully import my project to the test-suite in my project, as well as being able to run the program from the command-line. I've been able to run my test-suite for some time, under the impression that if the tests work, so does the command-line stuff--evidently this isn't the case. I do not yet intend on using my program as a library. The api.py acts is the entry-point for the program.
I have a project with the following structure (the same directory hierarchy as requests):
myapp/
myapp/
__init__.py
api.py # depends on commands.py
commands.py # depends on utils.py
utils.py
tests/
context.py
test_api.py # depends on api.py
test_commands.py # depends on commands.py, utils.py
In the file context.py I have a path modification adding myapp to the PYTHONPATH, so I can successfully run the tests on my code. Here is the contents of that file
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import myapp
I've tried imaginable import combination I can think of. Far too many to list! I have also perused the Python reference import system page, and this tutorial.
How should I import my dependencies?
Turns out this was the correct layout, I mistook the error for something else. Although for future reference, relative imports in Python 3 must be explicit: when in the myapp package directory you can't say import commands, you must instead import it as from . import commands. This was defined in PEP 328 also see this SO post on the topic. Run your package with python -m mutil.api not python ./mutil/api.py as the latter won't give the interpreter context of the current path.

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...

Python import modules, folder structures

I have been looking for a way to solve this.
I have a python project, and this is the folder structure I want:
/project/main.py
/project/src/models.py
/project/test/tests.py
I want to be able to run the tests by executing the tests.py in terminal. tests.py imports modules in /project/src/ for testing. First I solved this by adding
sys.path.insert(0, '..') in tests.py. But then the paths used in models.py for opening text files had to be relative to the tests.py, etc. Which means the program wouldn't run when excecuted from main.py, cause of the paths.
I also tried with dots when importing modules into tests.py, like from ..src.models import *, but that gave error message saying "Attempted relative import in non-package".
What should I put in the top of tests.py to be able to import the modules from models.py?
The structure you're using is not one I would recommend, but I'm a comparaitive newb to how Python projects are usually structured. I believe this will do what you're after:
1) Place an __init__.py file inside /project, /project/src, and /project/test to make sure they're treated as packages.
2) Place from __future__ import absolute_import at the top of each Python file.
3) Then use relative imports:
test.py:
from ..src import models
main.py:
from .src import models
4) You'll need to start your application differently. Ensure your current directory is the parent of /project (which appears to be the file system root) and run your project this way:
python -m project.main
For my own project, I would definitely put main.py inside src if it's the start point of your application. I might put tests.py in src, too, but if not, I would add /project/src to the test runner's Python path on the command line instead of in code.
I would still use absolute_import regardless. In my experience, it's a very clean solution to module organization, and it's also how things work by default in Python 3.
first put your main.py in the src directory..
in your tests you can do , sys.path.append('the src directory')
if you like to force the execution to be in specific directory regardless from where you are executing the app i suggest you adding
import os
os.chdir('relative path to the src dir')
thisway your program will run in the directory you specified so it will respect the relative paths you have in your code

Categories