-1

I have a config.yaml file that I want to import in my app.py.

This is my folder structure:

 /root
      __init__.py
      app.py
      config.yaml

App.py:

   import logging
   import config
   from flask import Flask

   app = Flask(__name__)
   logger = logging.getLogger(__name__)

   app.run(port=5001)

I have no problem importing other Python modules (.py) but when I try to import my YAML file, i get:

  ImportError: No module named config

Does anyone know why this does not work? Do I need any dependencies to be able to import a YAML file like this?

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Cheese Puffs
  • 945
  • 3
  • 10
  • 19
  • Why did you expect to be able to import a YAML file? It's not a Python module! What's in it? Have you tried searching for a Python YAML parser? – jonrsharpe Jun 05 '16 at 07:49

2 Answers2

2

Why do you expect to be able to import a YAML file? It isn't Python, and it isn't an extension module, and these are the only things that Python will import.

YAML (Yet Another Markup Language) is a data format, so you have to read the data with a suitable library: this answer might give you some clues.

Community
  • 1
  • 1
holdenweb
  • 27,899
  • 7
  • 50
  • 73
  • 1
    Thanks. Yes, I forgot to mention that in my post. I do have that in my __init__ file. I thought that parser would make me able to import my 'config' in other .py files. – Cheese Puffs Jun 05 '16 at 07:57
0

You cannot import YAML files directly into Python as you suggest in your example - only *.py files. You can import the yaml Python module and parse your file, as shown in these SO answers:

How can I parse a YAML file in Python

Parsing a YAML file in Python, and accessing the data?

Community
  • 1
  • 1
Simon Steinberger
  • 6,365
  • 5
  • 50
  • 93