开发者

Access to aliased functions in a Python module

开发者 https://www.devze.com 2023-01-12 05:49 出处:网络
I am consolidating many shell-like operations into a single module.I would then like to be able to do:

I am consolidating many shell-like operations into a single module. I would then like to be able to do:

pyscript.py:
from shell import *
basename("/path/to/file.ext")

and the shell.p开发者_开发问答y module contains:

shell.py:
from os.path import basename

The problem is that functions imported to the shell module are not available, since the import statement only includes functions, classes, and globals defined in the "shell" module, not ones that are imported.

Is there any way to get around this?


Are you sure you're not just using the wrong syntax? This works for me in 2.6.

from shell import * 
basename("/path/to/file.ext")

shell.py:

from os.path import basename


It's not a bug, it's a feature :-)

If you import m1 in a module m2 and then import m2 into another module, it will only import things from m2, not from m1. This is to prevent namespace pollution.

You could do this:

shell.py:

import os.path
basename = os.path.basename

Then, in pyscript.py, you can do this:

from shell import * # warning: bad style!
basename(...)
0

精彩评论

暂无评论...
验证码 换一张
取 消