简体   繁体   中英

Python: How to Import a folder as a module

I am new to Python so bear with me.

I have recently download the pdblp module in a zip form. I then made some amendments that were needed to get through specific requests. So I have this folder, is there a way where I normally write Import pdblp that I can write import 'F:\\Sam\\project\\' as pdblp or should it not be done like this?

You can import folder as package not module. *.py file is itself module. If you want to import folder, you need add init .py file inside the folder and then it will become package then you can import

Assuming that the module pdblp is located inside your project-directory, you can simple append your current path and then import it afterwards like so:

import sys
# Keep in mind that you need to escape backslashes by doubling them
sys.path.append("F:\\Sam\\project\\")

import pdblp

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