Module Conflicts

J

Jose

I have a module named math.py in a package with some class
definitions. I am trying to import the standard python math module
inside of math.py but It seems to be importing itself. Is there any
way around this problem without renaming my math.py file?
 
B

Benjamin

I have a module named math.py in a package with some class
definitions. I am trying to import the standard python math module
inside of math.py but It seems to be importing itself. Is there any
way around this problem without renaming my math.py file?

Not without some unpythonic magic. It's really not good style to name
a module the same as a stdlib one. It'll also confuse people reading
your code.
 
J

Jose

Not without some unpythonic magic. It's really not good style to name
a module the same as a stdlib one. It'll also confuse people reading
your code.

Yeah but I thought since math.py was in a package, it would be okay.
It's no big deal. I'll just rename my module :(
 
T

Terry Reedy

| >
| > > I have a module named math.py in a package with some class
| > > definitions. I am trying to import the standard python math module
| > > inside of math.py but It seems to be importing itself. Is there any
| > > way around this problem without renaming my math.py file?
| >
| > Not without some unpythonic magic. It's really not good style to name
| > a module the same as a stdlib one. It'll also confuse people reading
| > your code.
|
| Yeah but I thought since math.py was in a package, it would be okay.

The stdlib contains a few packages, but it is not a package in itself.
So math is not in a package.
 
I

Ivan Illarionov

I have a module named math.py in a package with some class
definitions. I am trying to import the standard python math module
inside of math.py but It seems to be importing itself. Is there any
way around this problem without renaming my math.py file?

Yes, if you are using Python 2.5

from __future__ import absolute import

after this `import math` will always import standard math module and
`from . import math` will import your module.
 
I

Ivan Illarionov

Yes, if you are using Python 2.5

from __future__ import absolute import

after this `import math` will always import standard math module and
`from . import math` will import your module.

I should say `from __future__ import absolute_import`

And it's relatively easy to do in earlier versions too:
create subdirectory `stdmath` with one `__init__.py` file with one
line `import math` and than use `from stdmath import math`.
and than use
 
S

Steve Holden

Terry said:
| >
| > > I have a module named math.py in a package with some class
| > > definitions. I am trying to import the standard python math module
| > > inside of math.py but It seems to be importing itself. Is there any
| > > way around this problem without renaming my math.py file?
| >
| > Not without some unpythonic magic. It's really not good style to name
| > a module the same as a stdlib one. It'll also confuse people reading
| > your code.
|
| Yeah but I thought since math.py was in a package, it would be okay.

The stdlib contains a few packages, but it is not a package in itself.
So math is not in a package.
Indeed, the fact that there is a math.py indicates that it is a module:
a package would have been math/__init__.py.

For extra marks, what does the interpreter do with "import x" when there
is both an x.py and an x/__init__.py in the same directry on the path?

regards
Steve
 
G

Gabriel Genellina

En Thu, 10 Apr 2008 17:41:29 -0300, Ivan Illarionov
And it's relatively easy to do in earlier versions too:
create subdirectory `stdmath` with one `__init__.py` file with one
line `import math` and than use `from stdmath import math`.

Ah, thanks, it seems that the idea can be extended to almost all the
standard library.
Create a directory "stdlib" somewhere on sys.path, with a single file
__init__.py containing this single line:

__path__.append("path/to/python/standard/lib")

Now, `import stdlib.gzip` will import the standard gzip module, even if
there is a gzip.py in the current directory or in some other place along
sys.path.
That is, we have made a package out of the standard library.
Unfortunately it doesn't work for math as-is because math is a builtin
module (at least on Windows), but this should work for all other "normal"
Python-level modules.
(It's a hack, and I would never use this on production code, but it may be
useful sometimes)
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top