覆盖 Python 中的命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1096216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:28:42  来源:igfitidea点击:

Override namespace in Python

pythonpython-import

提问by Art

Say there is a folder, '/home/user/temp/a40bd22344'. The name is completely random and changes in every iteration. I need to be able to import this folder in Python using a fixed name, say 'project'. I know I can add this folder to sys.path to enable import lookup, but is there a way to replace 'a40bd22344' with 'project'?

假设有一个文件夹“/home/user/temp/a40bd22344”。该名称是完全随机的,并且在每次迭代中都会发生变化。我需要能够使用固定名称在 Python 中导入此文件夹,例如“项目”。我知道我可以将此文件夹添加到 sys.path 以启用导入查找,但是有没有办法用“项目”替换“a40bd22344”?

Maybe some clever hacks in init.py?

也许init.py 中有一些巧妙的技巧?

Added:

添加:

It needs to be global - that is, other scripts loading 'project' via the standard:

它需要是全局的——也就是说,其他脚本通过标准加载“项目”:

import project

Have to work properly, loading a40bd22344 instead.

必须正常工作,改为加载 a40bd22344。

回答by ars

Here's one way to do it, without touching sys.path, using the impmodule in Python:

这是使用impPython 中的模块的一种方法,无需触及 sys.path :

import imp

f, filename, desc = imp.find_module('a40bd22344', ['/home/user/temp/'])
project = imp.load_module('a40bd22344', f, filename, desc)

project.some_func()

Here is a link to some good documentation on the impmodule:

以下是该imp模块的一些优秀文档的链接:

回答by krawyoti

You first import it with import:

你先用导入进口

>>> __import__('temp/a40bd22344')
<module 'temp/a40bd22344' from 'temp/a40bd22344/__init__.py'>

Then you make sure that this module gets known to Python as project:

然后你确保这个模块被 Python 知道为project

>>> import sys
>>> sys.modules['project'] = sys.modules.pop('temp/a40bd22344')

After this, anything importing project in the current Python session will get the original module

在此之后,当前 Python 会话中的任何导入项目都将获得原始模块

>>> import project
>>> project
<module 'temp/a40bd22344' from 'temp/a40bd22344/__init__.py'>

This will work also for sub-modules: if you have a foobar.py in the same location you'll get

这也适用于子模块:如果您在同一位置有一个 foobar.py,您将获得

>>> import project.foobar
>>> project.foobar
<module 'project.foobar' from 'temp/a40bd22344/foobar.py'>

Addendum.Here's what I'm running:

附录。这是我正在运行的内容:

>>> print sys.version
2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]

回答by Alex Martelli

Sure, project = __import__('a40bd22344')after sys.pathis set properly will just work.

当然,正确设置project = __import__('a40bd22344')后就sys.path可以了。

Suppose you want to do it in a function taking the full path as an argument and setting the globalimport of projectproperly (as well as magically making import projectwork afterwards in other modules). Piece of cake:

假设您想在一个以完整路径为参数并正确设置全局导入的函数中执行此操作project(以及import project随后在其他模块中神奇地进行工作)。小菜一碟:

def weirdimport(fullpath):
  global project

  import os
  import sys
  sys.path.append(os.path.dirname(fullpath))
  try:
      project = __import__(os.path.basename(fullpath))
      sys.modules['project'] = project
  finally:
      del sys.path[-1]

this also leaves sys.path as it found it.

这也让 sys.path 找到了它。