473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

file / module / package - import problem

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
2 1344

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
aspineux wrote:
import os.path

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sdhyok | last post by:
Here is my situation. To add more functionalities to a standard library(datetime) in python, I am implementing my own code called vp.datetime (directory structure, vp/datetime.py). To make it...
3
by: George P | last post by:
I've run into a strange package related namespace problem. Follow these steps (on UNIX system) to illustrate: ------------------------- mkdir /tmp/mypkg cd /tmp/mypkg touch __init__.py echo...
1
by: DierkErdmann | last post by:
Hi ! I am trying to create an exe file using pyinstaller. Running the created exe-File gives the error message "" Traceback (most recent call last): File "<string>", line 8, in <module> File...
10
by: HCB | last post by:
Hello: The book "Code Complete" recommends that you put only one class in a source file, which seems a bit extreme for me. It seems that many classes are small, so that putting several of them...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.