简体   繁体   English

在一个函数中导​​入多个python模块,以轻松设置熟悉的环境

[英]Import multiple python modules in one function to easily setup a familiar environment

I use a lot of Jupyter notebooks for my work to design systems. 我在设计系统时使用了很多Jupyter笔记本。 I find myself doing the same things to set up my environment for each notebook I start, soo I figured I would make a function that does all that for me. 我发现自己在做同样的事情来为我启动的每个笔记本电脑设置环境,所以我想我会创建一个为我做所有事情的函数。 It should import a list of modules into the desired namespace. 它应将模块列表导入所需的名称空间。 For instance, say this function exists at: my_package.setup_workspace: 例如,假设此函数存在于:my_package.setup_workspace:

def imports():
    import numpy as np

I would then like to be able to setup my workspace on a new jupyter notebook with: 然后,我希望能够使用以下命令在新的Jupyter笔记本上设置工作区:

import my_package
my_package.setup_workspace
np.array(["I wish this could work!"])

But when I do that, python reports that it can't find np, so the imports() function wasn't able to properly import numpy into the new jupyter workspace. 但是当我这样做时,python报告它找不到np,因此import()函数无法将numpy正确导入到新的jupyter工作区中。

When you do an import in a function you import things in the local namespace of the function, not the module global one. 在函数中进行导入时,您将在函数的local名称空间中而不是模块global名称空间中导入内容。 Unless you declare that the variable are global, but then they need to already exit in outer scope. 除非您声明变量是全局变量,否则它们必须已经在外部范围中退出。 This allow you to reuse variable name across function without "contaminating" outer scope. 这使您可以在不“污染”外部作用域的情况下跨函数重用变量名。

You have a couple ways of going forward: 您有几种前进的方式:

First self contained way see this twitter thread and this solution, to define your own magic. 第一种自包含的方式请参阅 Twitter线程和解决方案,以定义您自己的魔术。 Then you can then do %imports in a notebook to replace the current cell by a bunch of imports. 然后,您可以在笔记本中执行%imports ,以一堆导入替换当前单元格。 Advantage is you can pass options, but it works only in IPython. 优点是您可以传递选项,但仅在IPython中有效。

Second, simply remove the function and do all your setup at the top level of my_package.py , then do from my_package import * . 其次,只需删除该函数并在my_package.py的顶层进行所有设置,然后from my_package import * import * are frowned upon in the Python world, but if it's just for you, you should be safe. import *在Python世界中不受欢迎,但是如果只适合您,则应该是安全的。

Third, look at what %run -i in IPython can do, it's hackish but should work, as it can execute and external scripts in the current namespace. 第三,看一下IPython中的%run -i可以做什么,虽然有点棘手,但是应该可以工作,因为它可以执行当前名称空间中的脚本和外部脚本。

There is a couple of other crazy way you can investigate once you want to dive deep into how python is working: You can, for example actually manually extract setup_workspace 's __code__ object; 一旦想深入了解python的工作方式,您还可以通过其他两种疯狂的方式进行调查:例如,您可以手动提取setup_workspace__code__对象。 modify it and re-exec it to not create a local scope, but that's probably not what you'd looking for. 修改并重新EXEC它不创建一个本地范围,但是可能不是你想要找什么。 It is possible though, but may not be portable across Python versions. 虽然有可能,但可能无法跨Python版本移植。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM