简体   繁体   中英

Importing custom library from different folder path in python

I have the following folder structure:

A/fold1/MyLib/package/

in which I have an __init__.py file and a file with the functions I need, let's call it file.py .

When I work from this path:

A/fold1/myProject/script.py

I try to import it but it looks like this library can't be found.

I have already worked, and it was fine when I had a structure like this:

A/fold1/myProject/package/file.py

where package contained also the __init__.py file.

I also got suggestions from the Spyder IDE. What I used, to make it work was to import like this:

if packagePath not in sys.path:

    sys.path.append(packagePath)

import package.file as f

But currently it doesn't work, and I don't even get the suggestions from the Spyder IDE.

Is there a way to import from the specified structure?

I wanted a folder with all my functions, and if possible to have it in this folder and not between the hundreds of libraries of python.

Thanks!

Not enough points to comments, but this question is a duplicate. Already answered here: https://stackoverflow.com/a/4383597/12197595

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