0

According to this, I am trying to run my flask web application from my com_profiler directory as python -m api.index, python -m api.index.py, python api/index.py. But none of these are working. The errors I am getting are in the order -

-> ValueError: attempted relative import beyond top-level package

-> attempted relative import beyond top-level package

-> SystemError: Parent module '' not loaded, cannot perform relative import

Directory Structure:

comp_profiler/
├── api
│   ├── bootstrap.sh
│   ├── index.py
│   └── __init__.py
├── __init__.py
├── pipelines.py
├── random_useragent.py
├── requirements.txt
├── scrapy.cfg
├── spiders
│   ├── __init__.py
│   ├── content_handler.py
│   ├── core_spider.py
│   ├── middlewares.py
│   ├── scrapper
│   │   ├── __init__.py
│   │   ├── corporatedir.py
│   │   ├── craft.py
│   │   ├── tofler.py
│   │   └── zaubacorp.py
│   ├── scrapper.py
│   ├── seed_list_generator.py
│   ├── settings.py
│   └── utility.py

index.py

from flask import Flask, request, jsonify
from ..spiders.seed_list_generator import SeedListGenerator

app = Flask(__name__)

@app.route("/start-spider")
def hello_world():
    spider = SeedListGenerator()
    company_name = request.get_json()
    print(company_name)
    return "Hello world"


if __name__ == "__main__":
    app.run()

I also tried running it from api directory, but no success.

Kindly let me know if I am missing something with flask setup, as I am just starting up with flask.

Update: I want to integrate SeedListGenerator as API call. Kindly suggest.

Thanks in advance.

Om Prakash
  • 2,675
  • 4
  • 29
  • 50
  • Either you can restructure your directory layout such that you don't need `..` import or you can turn the whole code base into a package. In any case you should have a separate script that invokes the app, i.e. the `..spiders` should be a `.spiders`. – a_guest Mar 09 '18 at 14:08
  • By `package` in python, every directory should have empty `__init__.py`. Right? Please correct me if I got it wrong. – Om Prakash Mar 09 '18 at 14:14
  • Yes but I mean you could also install the package (locally) via `pip` for example. But in any case, I would have that `index.py` script at the very top level, that is instead of inside the `api` directory it should go to `comp_profiler`. The main script invoking the other parts should sit atop. – a_guest Mar 09 '18 at 14:17
  • What about `python -m comp_profiler.api.index`? If `api` is your top-level package you can't make relative imports beyond that. – yorodm Mar 09 '18 at 15:06

0 Answers0