简体   繁体   English

从其他目录导入自定义模块

[英]Import custom module from other directory

Let's say I have a custom module in: 假设我有一个自定义模块:

/basexx_yy/subdir1/subdir2/subdir3/subdir4/custom_module.py

And the script which needs to import custom_module.py is located at: 需要导入custom_module.py的脚本位于:

/basexx_yy/subdir1/otherdir/script.py

The basexx_yy is a dir with a dynamic name, consider xx and yy to be time stamps, let's say for the sake of clarity that xx is day of the week (01 = Monday - 05 = Friday) and yy is week number ( subdir1 - subdir4 are constant). basexx_yy是一个具有动态名称的目录,以xxyy为时间戳记,为清楚起见,假设xx是星期几(01 =星期一-05 =星期五),而yy是星期数( subdir1 subdir4是常量)。 So the full path to custom_module.py cannot be included as a static address. 因此,无法将custom_module.py的完整路径作为静态地址包括在内。 Since the subdirs are constant, I wrote the following code: 由于子目录是常量,因此我编写了以下代码:

import os
import sys
cwd = os.getcwd()
split = cwd.split('\\')
if 'subdir1' in split:
    parentdir = cwd.split('subdir1')
    sys.path.insert(0, os.path.join(parentdir[0], 'subdir1', 'subdir2', 'subdir3', 'subdir4'))
else:
    sys.exit("'subdir' dir not found! Run the script from within basedir.")

import custom_module

It does not, however, work. 但是,它不起作用。 I'd appreciate some clarity as I cannot see why this doesn't work. 我会很清楚,因为我看不到为什么这不起作用。

It is not exactly clear from your question what your situation is, but here goes as it was designed to be used : 从您的问题中尚不清楚您所处的情况,但是按照设计使用的方式进行

  • A package contains a setup.py and can be installed using pip . 软件包包含setup.py ,可以使用pip进行安装。
  • A module lives inside a package or a module and contains an __init__.py . 模块位于包或模块内部,并包含__init__.py

Your package should have a structure like 您的包裹应具有以下结构

setup.py
basexx_yy/
          __init__.py
          something_else.py
          subdir1/
                  __init__.py
                  more_files.py
                  otherdir/
                           script.py
                  subdir2/
                          __init__.py
                          etc.py
                          subdir3/
                                  __init__.py
                                  pp.py
                                  subdir4/
                                          __init__.py
                                          custom_module.py

basexx_yy being a package means it can be installed into your python library collection using basexx_yy是一个软件包,意味着可以使用以下命令将其安装到您的python库集合中

pip install basexx_yy

or, while developing 或者,在开发时

pip install -e basexx_yy

Afterwards any script (it may even be completely outside your package) can do 之后,任何脚本(甚至可能完全在您的软件包之外)都可以执行

import basexx_yy

and also deep import like 而且像

import basexx_yy.subdir1.subdir2.subdir3.subdir4.custom_module as cm

cm.sqrt(4)

or, any file in your module tree ( otherdir is missing __init__.py so it is outside the module tree and cannot do this) can do relative imports. 或者,模块树中的任何文件( otherdir缺少__init__.py因此它在模块树之外,无法执行此操作)可以进行相对导入。

eg custom_module.py can do 例如custom_module.py可以做

from . import custom_modules
from .. import pp
from ... import etc
from .... import more_files
from ..... import something_else

I agree with Nils Werner, that you most probably want to restructure your package(s). 我同意Nils Werner的观点,您很可能希望重组您的软件包。

Anyway, your code should work! 无论如何,您的代码应该可以工作! Only problem is that you use 唯一的问题是您使用

split = cwd.split('\\')

instead of 代替

split = cwd.split(os.path.sep)

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

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