简体   繁体   English

如何在python中导入我自己的包?

[英]How can I import my own package in python?

I have a project我有一个项目

testci/
├── __init__.py
├── README.md
├── requirements.txt
├── src
│   ├── __init__.py
│   └── mylib.py
└── test
    ├── __init__.py
    └── pow_test.py

When I run python3.6 test/pow_test.py I see an error:当我运行python3.6 test/pow_test.py我看到一个错误:

File "test/pow_test.py", line 3, in import testci.src.mylib as mylib ModuleNotFoundError: No module named 'testci'文件“test/pow_test.py”,第 3 行,在 import testci.src.mylib as mylib ModuleNotFoundError: No module named 'testci'

pow_test.py pow_test.py

from testci.src.mylib import get_abs


def test_abs():
    assert get_abs(-10) == 10 

How can I fix this error?我该如何解决这个错误?

System details: Ububntu 16.04 LTS, Python 3.6.10系统详细信息:Ububntu 16.04 LTS、Python 3.6.10

try this尝试这个

from  .src import  mylib
from mylib import get_abs

if it won't work then import one by one.如果它不起作用,则一一导入。 But don't import the root folder since the file you are importing to is on the same folder you are trying to import then it will always raise an error但是不要导入根文件夹,因为您要导入的文件位于您尝试导入的同一文件夹中,那么它总是会引发错误

Run Python with the -m argument within the base testsci package to execute as a submodule.使用基本testsci包中的-m参数运行 Python 以作为子模块执行。

I made a similar mock folder structure:我做了一个类似的模拟文件夹结构:

├───abc_blah
│   │   abc_blah.py
│   │   __init__.py
│
└───def
    │   def.py
    │   __init__.py

abc_blah.py abc_blah.py

print('abc')

def.py定义文件

import abc_blah.abc_blah

Execute like such:像这样执行:

python -m def.def

Correctly prints out 'abc' as expected here.在此处按预期正确打印出 'abc'。

simply add __package__ = "testci" and also it is a good practice to add a try and except block只需添加__package__ = "testci"并且添加try and except块也是一个好习惯

Your final code should look something like你的最终代码应该看起来像

try:
    from testci.src.mylib import get_abs
except ModuleNotFoundError:
    from ..testci.src.mylib import get_abs

for running it, type python -m test.pow_test要运行它,请键入python -m test.pow_test

I think your issue is how the package is installed.我认为您的问题是软件包的安装方式。 The import looks fine to me.导入对我来说很好。 As it says CI I'm guessing you're having the package installed remotely with only the test folder somehow.正如它所说的 CI 我猜你正在远程安装包,只有 test 文件夹。

Try adding a setup.py file where you define that both the test as well as the src packages are part of your testci package.尝试添加一个setup.py文件,在其中定义testsrc包都是testci包的一部分。

there are many ways to organize a project, keep things consider in mind, structure should be simple and more scaleable, can differentiate the things in codebase easily.项目的组织方式有很多种,三思而后行,结构要简单,更具伸缩性,可以轻松区分代码库中的事物。

one of the few good possible ways to structure a project is below下面是构建项目的少数好方法之一

project/
├── app.py
├── dockerfile
├── pipfile
├── Readme.md
├── requiements.txt
├── src_code
│   ├── code
│   │   ├── __init__.py
│   │   └── mylib.py
│   └── test
│       ├── __init__.py
│       └── test_func.py
└── travisfile

here app.py is main file which is responsible to run your entire project这里app.py是负责运行整个项目的主文件

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

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