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
intended for use as the main module
of a Python application should always use absolute imports."
The distinction between modules and scripts was new to me. One of the
primary features I always appreciated in Python was that modules had a
dual purpose: they were components and they were programs. One could
use them in isolation or as a part of a package something I always
missed when I programmed in languages with the tyranny of a single
main.
However 'implicit relative imports' work as usual and Py3K just
changes the priority of search - one might simply ignore the
'explicit' variant and care for unambiguous names.
Now things are not so easy in life and when you are confronted with
other peoples software you have to know at least the rules of the
game.
Digging a little deeper I found the following section in "What's new
in Python 2.6"
'Python’s -m switch allows running a module as a script.
When you ran a module that was located inside a package, relative
imports didn’t work correctly.'
Hmm... they worked as specified or at least as documented. I admit I'm
confused but I suspect I'm not the only one. Now everything is fine.
You just run each and every module with the -m option "as a script"
because there is a chance that somewhere is a relative import ( e.g.
local to a function ) and suddenly the module fails to import stuff
due to a modality in the import behavior.
Let's see how it works in practice.
This here my very first attempt to start Pythons 2to3 refactoring tool
written by the master himself. I looked at it and he used relative
imports.
C:\Python30\Lib \lib2to3>C:\Pyt hon30\python -m refactor.py
Traceback (most recent call last):
File "C:\Python30\li b\runpy.py", line 103, in _run_module_as_ main
loader, code, fname = _get_module_det ails(mod_name)
File "C:\Python30\li b\runpy.py", line 78, in _get_module_det ails
loader = get_loader(mod_ name)
File "C:\Python30\li b\pkgutil.py", line 456, in get_loader
return find_loader(ful lname)
File "C:\Python30\li b\pkgutil.py", line 466, in find_loader
for importer in iter_importers( fullname):
File "C:\Python30\li b\pkgutil.py", line 422, in iter_importers
__import__(pkg)
File "C:\Python30\li b\lib2to3\refac tor.py", line 23, in <module>
from .pgen2 import driver
ValueError: Attempted relative import in non-package
Not working. I'm perfectly sure I don't understand the error message
but what's worse is that runpy.py fails which
was just created ( in Python 2.6 ) to fix the issue with the
apparently broken relative imports that is o.k. according to the
introductory material that is dedicated to newbies.
I'm trying to fix this naively by turning the explicit into implicit
relative (or absolute?) imports and see what happens.
refactor.py
------------------------
...
# Local imports
from pgen2 import driver
from pgen2 import tokenize
import pytree
import patcomp
import fixes
import pygram
....
C:\Python30\Lib \lib2to3>C:\Pyt hon30\python -m refactor.py
Traceback (most recent call last):
File "C:\Python30\li b\runpy.py", line 103, in _run_module_as_ main
loader, code, fname = _get_module_det ails(mod_name)
File "C:\Python30\li b\runpy.py", line 78, in _get_module_det ails
loader = get_loader(mod_ name)
File "C:\Python30\li b\pkgutil.py", line 456, in get_loader
return find_loader(ful lname)
File "C:\Python30\li b\pkgutil.py", line 466, in find_loader
for importer in iter_importers( fullname):
File "C:\Python30\li b\pkgutil.py", line 422, in iter_importers
__import__(pkg)
File "refactor.p y", line 27, in <module>
import patcomp
File "C:\Python30\li b\lib2to3\patco mp.py", line 17, in <module>
from .pgen2 import driver
ValueError: Attempted relative import in non-package
This time patcomp fails with its relative imports despite the fact
that it is not imported as a "script" but as a module inside the
lib2to3 package and it is not __main__ but refactor.py is.
I give up. I always thought migration from Python 2.X to Python 3.0
was an issue not the conversion tool ;) 0 1398 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rutger Claes |
last post by:
In the directory /var/www/dynamo/xsl is the basic xsl file for xhtml
documents /var/www/dynamo/xsl/xhtml.xsl. In this file there is an import
statement: <xsl:import href="xhtml/basic.xhtml.xsl" />.
This import statement should load /var/www/dynamo/xsl/xhtml/basic.xhtml.xsl
as far as I know.
The current document is /var/www/dynamo/xml/index.xml (.xml documents are
handled by the PHP parser on my server). This is the error message I get:...
|
by: Randall Smith |
last post by:
I've noticed the push by Guido and others to use absolute imports
instead of relative imports. I've always enjoyed the ease of relative
imports, but am starting to understand that "explicit is better than
implicitly" as the Python philosophy goes. I'm trying to develop a
strategy for writing packages using only absolute imports and would
really like to know what others have learned.
Randall
|
by: LesleyW |
last post by:
I'm using a public set of schemas for GML, avaiable at www.opengis.net/gml.
They all include or import each other but live in a folder hierarchy, so the
import statement includes schemaLocation="../xlink/xlinks.xsd". I've written
a schema that imports the top level one using
schemaLocation="http://schemas.opengis.net/gml/3.1.0/base/feature.xsd". I've
also tried downloading it and putting it in the schema cache but in both
cases the build...
|
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...
|
by: Alexey Borzenkov |
last post by:
After reading PEP-0328 I wanted to give relative imports a try:
# somepkg/__init__.py
<empty>
# somepkg/test1.py
from __future__ import absolute_import
from . import test2
if __name__ == "__main__":
| |
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::
|
by: rbygscrsepda |
last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import *
from any relative package name--i.e., a name that starts with a dot.
For instance, none of the following work:
from . import *
from .sibiling import *
from .. import *
from ..parent_sibling import *
....and so on. The same error occurs:
|
by: DG |
last post by:
Alright, I have searched and searched and read many conversations on
the topic of relative and absolute imports and am still not getting
the whole thing through my skull.
Highlights of what I've read:
http://mail.python.org/pipermail/python-list/2007-January/422973.html
http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1e8dc93471a7079/8751c82cfe1ca3f2?lnk=gst&q=absolute+import#8751c82cfe1ca3f2...
|
by: Gabriel Genellina |
last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com>
escribió:
Why don't you let the caller tell you its own location, using __file__?
The above code is too much magic for me.
Yes.
|
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,...
|
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...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |