0

I'm developing a webapp using Flask framework and i'm ready to ship it to production. For this scope i decided to use setup.py file with a WSGI server(instead of: export FLASK_APP=app.py && flask run).
However i don't known why python cannot import config.py(which include database connection parameters) file from the root of the project.

This is the project structure:

├── app
│  ├──__init__.py
│  ├──forms.py
│  ├──models.py
│  ├──routes.py
│  ├──server.py
│  ├──static
│  │  └──css
│  └──templates
│     ├──   base.html
│     ├──   [...]
├──app.py
├──config.py
└──setup.py

app,py:

from app import app

app/__init__.py:

from flask import Flask
# [...]
from config import Config

app = Flask(__name__)
app.config.from_object(Config)
# [...]

from app import routes, models

app/server.py:

# [..]
from .app import app
import os

# Define paths
CURRENT_DIR = os.path.abspath(os.getcwd())
MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_PATH = os.path.join(MODULE_DIR, 'static')
TEMPLATES_PATH = os.path.join(MODULE_DR, 'templates')

def main():
    app.template_folder = TEMPLATES_PATH
    app.static_folder = STATIC_PATH

    server = WSGIServer(("127.0.0.1", 5000)), app)
    server.serve_forever()

if __name__ == "__main__":
    main()

setup.py:

from setuptools import setup, find_packages

# [...]

setup(
    name="app",
    version="0.0.1",
    description="Flask app",
    long_description=getLongDescription(),
    long_description_content_type="text/markdown",
    url="",
    author="",
    author_email="",
    license="",
    packages=find_packages(exclude=("tests")),
    install_requires=getRequirements(),
    include_package_data=True,
    entry_points={
        "console_scripts": [
            "flaskapp=app.server:main"
        ]
    },
)

and this is the error i got when i try to run the project with: python setup.py build install && flaskapp:

Traceback (most recent call last):
  File "/home/user/flaskapp/venv/bin/flaskapp", line 11, in <module>
    load_entry_point('flaskapp==0.0.1', 'console_scripts', 'flaskapp')()
  File "/home/user/flaskapp/venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/user/flaskapp/venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
    return ep.load()
  File "/home/user/flaskapp/venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2443, in load
    return self.resolve()
  File "/home/user/flaskapp/venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2449, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/home/user/flaskapp/venv/lib/python3.7/site-packages/flaskapp-0.0.1-py3.7.egg/app/__init__.py", line 5, in <module>
    from config import Config
ModuleNotFoundError: No module named 'config'
beep
  • 1,057
  • 2
  • 11
  • 23

1 Answers1

0

I already encountered this kind of problem, it seems like your __init__.py file in the app folder can't access config.py because it is in a parent folder.

Maybe try this : export PYTHONPATH=../ it worked for me.

But it seems like :

import sys, os
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../')`

worked for other.

https://stackoverflow.com/a/10253916/10700539

Senseikaii
  • 77
  • 1
  • 9