473,651 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <st**********@g mail.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_fil ename
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 1489

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

Similar topics

1
1620
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 do `./foo.py`, some "local" imports fails, I guess because current working directory is different. Is CWD the only difference between running `python foo.py` and `./foo.py`? What is the recommended way? (I guess security is one aspect that can...
3
1252
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 move the pages to a sub directory, which in turn broke all my links. Ok well I could go over the 100 or so links by hand and change them to ../images/ instead of images/ and prefix all my usercontrols with ../ but there is still trouble. Because...
18
9436
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 works, and sometimes I need to change it to <Assembly: AssemblyKeyFile("..\..\NicksKey.snk")>
1
4238
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). An XSLT script is called via a JSP to convert XML to HTML (<xsl:apply nameXml="routesummary" xsl="/xsl/routesummaryheader.xsl" />). This routesummaryheader.xsl script then needs to call another XSLT
0
1453
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 classical import syntax becomes absolute import only, so that all imports are explicitly one or the other. You can only use relative import from within packages. Trying to import a top-level module relatively from within same directory, produces...
0
1130
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
2728
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
1389
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 them. Opening the tutorial I found following notice ( 6.4.2 ): "Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules
0
1207
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 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
0
8361
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7299
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.