0

I have a repository with a handful of Python scripts (ETL jobs) that connect to various APIs (Google Analytics, Amazon, etc.). I'm using a virtual environment to keep track of dependencies when deploying this repo to a VPS instance. I also have a CONFIG.py file with various API keys, database credentials, etc. (that I keep out of version control).

Just recently I needed to connect to the Google DFP API. There's a known bug that when you install the googleads library (using pip) it will install a newer version of the oauth2client library, which will break my Google Analytics API scripts because the google-api-python-client uses an older version of oauth2client.

So, to overcome this obstacle I'm considering creating a sub directory in my repo called, Google_DFP and creating a new virtual environment that uses the newer version of oauth2client. Here is the new structure of my repo:

/ETL_Scripts
    CONFIG.py
    google_analytics.py
    google_search_console.py
    amazon_affiliate.py
    /venv
    /Google_DFP
      google_dfp.py
      /venv2

In my google_analytics.py file I use this to import the credentials from CONFIG import DB_CREDENTIALS and then use that object to connect to my database (username, password, etc.).

My question is: how do I import variables out of the CONFIG.py file (in the parent directory) within the google_dfp.py file? When I attempt to import variables out of CONFIG.py -- from ..CONFIG import DB_CREDENTIALS -- I get this error: ValueError: Attempted relative import in non-package.

Here's what my CONFIG.py file looks like:

AMAZON_API_KEY = "abcxyz123987"
DB_CREDENTIALS = {
    'database': 'fancy_reporting_db
    'user': 'etl_user',
    'host': 'localhost',
    'password': 'password'
}
Abundnce10
  • 2,150
  • 3
  • 26
  • 41
  • 1
    Is your project a package? I don't see any `__init__.py`s... See [this answer](http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python/73149#73149). – juanpa.arrivillaga Mar 28 '17 at 22:22
  • This project is not a package. – Abundnce10 Mar 28 '17 at 22:35
  • Then you can't use relative imports. You can do something like `import sys; sys.path.append("/path/to/.../ETL_Scripts")` at the top of your `google_dpf.py` file. Obviously, you would use the actual path to the `ETL_Scripts` directory... – juanpa.arrivillaga Mar 28 '17 at 22:38

0 Answers0