python 导入其他路径的模块_如何从其他目录导入多个Python模块

Python newbie here. Any and all help on python structure would be much appreciated!

MAIN QUES: How to Import Multiple Python Modules from Other Directories

I've reassembled the repository for a project so that modules for specific tasks are within individual folders according to type of task instead of just one huge folder. Because my modules were all in one folder, I used to be able to import easily in this fashion:

import sys

sys.path.insert(0,'/home/myname/folder_name/all_modules')

Now, I have four module folders.

aws_boto_modules

email_modules

gen_modules

mongo_modules

I want to import two of these modules into script_to_run.py.

What I tried that did not work:

import sys

sys.path.insert(0,'/home/myname/folder_name/email_modules')

sys.path.insert(0,'/home/myname/folder_name/gen_modules')

I tried sys.path.append as well.

I added to my PYTHONPATH on my local comp (MAC OS) and everything works dandy. However, when I change the PYTHONPATH on my AWS EC2 and attempt to run script_to_run.py, it cannot find email_modules nor gen_modules.

I've read a lot in the last three hours, but am still very confused about how to proceed. Any guidance, including useful links that I may not have encountered already, would be greatly appreciated. Thanks!

解决方案

__init__.py files are required to make Python treat the directories as containing packages. In the image below we are importing customer_info module from second_folder:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值