473,405 Members | 2,294 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,405 software developers and data experts.

Re: (relative) import trouble, sometimes it works, sometimes itdoesn't ...

En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <st**********@gmail.com>
escribió:
I'm running Python 2.5 and want my programs to run at least under
Windows and Linux (preferable also Mac).
So I guess I should always use relative paths.

From most modules I can call a global function,
that should import a dictionary from path deeper than the module itself.
The import is done in the global function.
In that global function, I get the modules path by

SourceFile = sys._getframe(1).f_code.co_filename
Why don't you let the caller tell you its own location, using __file__?
The above code is too much magic for me.
now to be sure to succeed the import (at least I was thinking this would
always be successful :-(
I need to
1- add the path of the module to be imported to sys.path
(or do a dot import)
Yes.
2- keep track of already done imports, to give a reload the next time
(or maybe always do an import followed by an reload ?)
A reload is only required if the module was changed on disk and you want
to get the later version.
Now what I don't understand is what relative path should I use in 1:
- relative to the main application
- relative to the working directory were I started the application
- relative to the current working directory
- relative to the module that is doing the import
- relative to the module that called the global function
Relative imports assume you are inside a *package*, and they're relative
to the current module location, each dot being one level above. See
http://www.python.org/dev/peps/pep-0328/
I would be pleased if someone could enlighten me,
because this information is hard to find.
Yes. How import works is very poorly documented, unfortunately. You'll
have to dig into the various PEPs and the bug tracker.

--
Gabriel Genellina

Oct 21 '08 #1
0 1468

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

Similar topics

1
by: Frans Englich | last post by:
Hello all, I have a couple of questions related to module importing. 1) When I start my Python program with `python foo.py` instead of simply adding a interpreter comment on the first line and...
3
by: tsteinke | last post by:
Okay so I had some aspx pages with document relative links to images and webusercontrols (which also have doc relative pats to images) I wanted to use forms authentication on, so I was forced to...
18
by: Nak | last post by:
Hi there, Does anyone know what path the AssemblyKeyFile attribute is relative to? For example I currently have mine set to <Assembly: AssemblyKeyFile("..\NicksKey.snk")> Sometimes this...
1
by: geoffblanduk_nospam | last post by:
Using Apache Tomcat 5.0.28 I'm having a problem with the xsl:import command. I have several Tomcat running on the same server (they could be different releases of code so can't share resources)....
0
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the...
0
by: Echo | last post by:
Here is my setup: rpg -objects --__init__.py --gameobject.py --material.py -__init__.py -run_tests.py -stats.py
12
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
0
by: Stef Mientki | last post by:
hello, I'm running Python 2.5 and want my programs to run at least under Windows and Linux (preferable also Mac). So I guess I should always use relative paths. From most modules I can call a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.