简体   繁体   English

如何跨项目导入python包

[英]How to import python packages across projects

I want to have a generic project in python which has packages and modules that can be used in multiple other projects. 我想在python中有一个通用项目,该项目具有可在多个其他项目中使用的包和模块。 I want to know how can I import generic projects packages and modules in other projects. 我想知道如何在其他项目中导入通用项目包和模块。 Say

A Generic Project which has re-usable/common code that can be used in multiple project 具有可重复使用/通用代码的通用项目,可在多个项目中使用

generic_project/
    package1/
        __init__.py
        module1/
    package2
        __init__.py
        module2/

and a Specific Project 和一个特定的项目

project_1
    package3/
        __init__.py
        module3/
    package4/
        __init__.py
        module4/

How can I import module1(of generic project) in module3 (project1) ? 如何在module3(project1)中导入(通用项目的)module1?
Just want to let you know that Generic project and Project 1 are two independent projects packaged and distributed separetly. 只想让您知道,通用项目和项目1是两个独立的项目,分别打包和分发。

I have gone through similar questions but all of them mostly talk about referring packages with in a project. 我经历过类似的问题,但所有这些人都大多谈论在项目中引用包。

  1. Both generic_project and project_1 (and the moduleX s) have to include a __init__.py file to be considered (and treated) as packages. generic_projectproject_1 (以及moduleX )都必须包含__init__.py文件,以将其视为(并视为)软件包。
  2. generic_project needs to be installed (or simply present) as a package in either package3 (near module3 ) or [python installation path]\\Lib , so the python interpreter will consider looking for the following expression. generic_project需要作为软件包安装(或简单地存在)在package3 (在module3附近)或[python installation path]\\Lib ,因此python解释器将考虑寻找以下表达式。
  3. You access it like every python package - from generic_project.package1 import module1 or import generic_project.package1.module1 . 您可以像访问每个python软件包一样访问它- from generic_project.package1 import module1import generic_project.package1.module1

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

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