简体   繁体   中英

When running python project from source, how to avoid conflict with installed package

I have been using an open source package installed via pip and running inside a Jupyter Notebook IPython environment.

I needed to modify the source of the package so I cloned the repo locally and made the changes and imported the local package instead of the installed package with no problem.

The problem I have is that inside the project source it imports its own package which is importing the installed package instead of the source project and causes a conflict in the code in a few cases .

Say the package name is PackageA , originally installed via pip install PackageA . I now have the source code for Package A in libs/ directory underneath my project directory. projectDir/libs/PackageA . I am importing the project via from libs.PackageA import PackageA as pa , however in the source code (in almost every file) it has import PackageA as pa .

I can go through the source and change every instance of import PackageA as pa with from .. import PackageA as pa but this occurs in probably over 100 files as some files have multiple import statements and its also very untidy as some files are in sub directories requiring from .... import PackageA as pa .

Is there a way to redirect the import to use the local version instead of the installed version (I have already uninstalled the pip installed version).

I am fairly new to python (not to software development) so is there a best practice for this type of setup as I'm sure its fairly common.

您可以在项目开始之前添加sys.path.append("../libs")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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