file / module / package - import problem

  • Thread starter EuGeNe Van den Bulke
  • Start date
E

EuGeNe Van den Bulke

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
 
A

aspineux

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'))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,416
Messages
2,571,562
Members
48,797
Latest member
shadowoftheunknown

Latest Threads

Top