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