Skip to content

Instantly share code, notes, and snippets.

@tsaoyu
Last active March 1, 2017 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaoyu/791d6c20962dbcc552eb29de725b3878 to your computer and use it in GitHub Desktop.
Save tsaoyu/791d6c20962dbcc552eb29de725b3878 to your computer and use it in GitHub Desktop.

I have a structure like this

├── core.py
├── D3HRE
│   ├── helpers.py
│   └── __init__.py
├── Pipfile
├── Pipfile.lock
├── README.md
├── resources
│   ├── __init__.py
│   ├── solar
│   │   ├── Data
│   │   │   └── global_radiation
│   │   ├── __init__.py
│   │   ├── SSEsolar.py
│   │   └── SSEsolarworking.py

SSEsolar.py is my solar radiation data processing script that read data from global_radiation. I use a very simple os path append way to read resources file.

filename = './Data/global_radiation'
current_path = os.path.abspath(os.path.dirname('__file__'))
file_path = os.path.join(current_path,filename)

with open(file_path) as f:
    data = f.readlines()

And I would like to achieve is in core.py

import resources.solar.SSEsolar

and then use all functions in SSEsolar.

And it is of course didn't works quite well because the relative path is not correct for core.py and I found your answer on stack-exchange http://stackoverflow.com/questions/5003755/how-to-use-pkgutils-get-data-with-csv-reader-in-python

In SSEsolar.py, I replace previous data read with the following:

data = pkgutil.get_data("Data", "lobal_radiation")
f = io.StringIO(data)
f.readlines()

But f is empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment