By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

file / module / package - import problem

P: n/a
Hi there,

I have a "problem" which could be a bad design on my behalf but I am not
sure so ...

I have a package WMI which contains a module hauteur.py which, when
imported, load data from a file located in WMI/data/. In hauteur.py I
call open('data/hauteur.yaml').

test.py
WMI/
hauteur.py
data/
hauteur.yaml
lot.py

It works well when hauteur is imported in lot.py but if I try import
WMI.hauteur in test.py it doesn't work because it looks for the
hauteur.yaml file in the "wrong" place.

Is there a way to tell a module in a package to look for a file in a
specific place i.e. a within package location?

Thanks,

EuGeNe -- http://www.3kwa.com
May 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

The filename and its path is in global variable __file__ (that is
different in any source file)

try

import os.path

file=open(os.path.join(os.path.dirname(__file__), 'hauteur.yaml'))


On 30 mai, 22:22, EuGeNe Van den Bulke <eugene.vandenbu...@gmail.com>
wrote:
Hi there,

I have a "problem" which could be a bad design on my behalf but I am not
sure so ...

I have a package WMI which contains a module hauteur.py which, when
imported, load data from a file located in WMI/data/. In hauteur.py I
call open('data/hauteur.yaml').

test.py
WMI/
hauteur.py
data/
hauteur.yaml
lot.py

It works well when hauteur is imported in lot.py but if I try import
WMI.hauteur in test.py it doesn't work because it looks for the
hauteur.yaml file in the "wrong" place.

Is there a way to tell a module in a package to look for a file in a
specific place i.e. a within package location?

Thanks,

EuGeNe --http://www.3kwa.com

May 30 '07 #2

P: n/a
aspineux wrote:
import os.path

file=open(os.path.join(os.path.dirname(__file__), 'hauteur.yaml'))
Thanks that worked ;)
May 31 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.