简体   繁体   English

Python:已导入的导入模块

[英]Python: Import module that has already been imported

I want to submit a pull request to a library ( imblearn , v.0.3.0) that is included in my python distribution (anaconda 4.3.14) by default. 我想向我的python发行版(anaconda 4.3.14)中默认包含的库( imblearn ,v.0.3.0)提交拉取请求。 Before submitting, I want to to test my cloned repo. 提交之前,我想测试克隆的仓库。 Therefore, I need to reload the module from a different location (cloned repo), instead of same the default location like described here . 因此,我需要从其他位置(克隆存储库)重新加载模块,而不是像此处所述的默认位置相同

Adding the location to the path as first element does not work: 将位置添加到路径作为第一个元素不起作用:

>>> import sys
>>> sys.path.insert(0, 'C:\\my repositories\\imbalanced-learn\\imblearn')

printing the version still gives the result from the version.py in the anaconda folder 打印版本仍然可以从anaconda文件夹中的version.py中获得结果

>>> from imblearn import version
>>> version.__version__
'0.3.0.dev0'

Is there a non hacky way ? 有没有非骇人听闻的方式?

I figured it out: 我想到了:

>>> import os, sys
>>> dir = os.path.dirname(os.path.abspath(os.path.realpath('.')))
>>> libRoot = os.path.join(dir, 'imbalanced-learn') # include parent folder of library
>>> sys.path.insert(0,libRoot) # NOTE: insert at beginning of path array

resulting into 导致

>>> from imblearn import version
>>> version.__version__
'0.3.1.pr'

assuming that the file structure is as follows and eg the jupyter notebook is started from the root folder: 假设文件结构如下,例如jupyter笔记本是从root文件夹启动的:

root
|- main.py
|- imbalanced-learn/
   |- imblearn/
      |- __init__.py
      |- setup.py
      |- ...

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

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