简体   繁体   中英

How to import file from another python project in PyCharm using project name

I have split my python code in different folders and I'd like to treat them as separate projects in PyCharm. Let's say one project (folder) is called 'lib' and I have lib/utils.py in it. The other one is called app and I have app/utils.py and app/app.py .

I know that I can add the project 'lib' as dependency for the project 'app' but then in app.py PyCharm only allows my import to look like:

from utils import some_util

I believe PyCharm treats app and lib as two source folders and adds app/utils.py to the PYTHONPATH as 'utils'.

When I deploy my app, I would preserve the same directory structure, hence I'd like to write my import like this:

from app.utils import some_util

So it works when I run it from the parent of the app and lib folders.

How can I use this import structure in PyCharm?

You may add init .py file in your folder to initial the module.

for ex. in folder utils, you can make your file like a.py, b.py and init .py

At the pycharm, you can load the module in the folder utils just import utils.a as a or from utils.a import utial_a

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