Reputation: 5307
Good Day - similar Python 3 import issues and question that I have seen in google and on this site, but no answer to this type of situation. How do I import a Flask-Classy view, from outside of the app?
Let's say I have a Flask app, on Python 3.6, using Flask-Classy and the structure looks something like:
MainFolder
├── __init__.py
├── app
│ ├── __init__.py
│ ├── views
│ │ ├── __init__.py
│ │ └── myview.py
│ │ └── config.py
│ ├── app.py
└── sideproj
├── __init__.py
├── calc.py
Now myview.py
imports config through from .config import Config
(Config being the classname within config.py
). myview.py
contains the main Class functions to work within the Flask framework but also has a helper
function within the class that does the main work. This is all good, and when I run python app.py
- it is able to serve the Flask App and I can access it at localhost:5000
.
Now my problem is, I am working on a side project that calculates some data and I want to use that core workhorse function within myview.py
but outside of the flask app. In sideproj/calc.py
I essentially want to import that myview.py
so I can run the function (currently sitting within the Class) without duplicating the code base.
Using an ugly hack the first thing I tried is:
import sys
sys.path.append('../app/views')
import myview
The code is able to import myview
but because myview.py
is importing config.py
it is not able to properly import it and gives me the error ImportError: attempted relative import with no known parent package
.
I can't find a way to import myview
with config.py
and still be able to run app.py
and serve the website. For example, if I change the import in myview.py
to just import config
, then calc.py
can run but app.py
fails due to it's inability to import Config
.
Is there a good way to do this? Or a way to re-organize that main workhorse function so it can be used by both myview.py
and calc.py
(and the functions reference values in config
). Ideally I don't have the same code in two spots.
Upvotes: 0
Views: 70
Reputation: 640
I would create a new package independent of your flask app and side-project that contains the workhorse functionality. For example, add a
workhorse_package
folder with asetup.py
that you install withpip install
. In the environment, you're running your flask app in. Then you can import that functionality within the flask app and side project code as needed but just maintain the functionality within theworkhorse_package
. @chris
You can use Flask blueprints. Whether this is applicable depends on your app usage.
(Hacky way, not recommended for a serious app, but can be useful when developing multiple prototypes, using some shared functionality) You can move myview.py
into MainFolder/helpers/
and write a script that makes a copy into both projects when running them.
Don't. If you want to do this to keep DRY, you don't. These are two separate projects, they deserve separate files. Even now they are different - one uses some config, the other doesn't. Is it really that helpful?
Upvotes: 1