首页 > 解决方案 > Installed package in project and getting error

问题描述

I have install package in my personal folder

sudo pip install boto3 -t ./lib

boto3 install in lib folder

Project 
 --> lib 
     --> boto3 package
     --> __init__.py
 --> python script
   --> read.py

lib has one package boto3

Now i am accessing boto3 in python script read.py import lib.boto3

Getting error 
  File "read.py", line 5, in <module>
    from lib import boto3
    File "/var/www/html/packages/lib/boto3/__init__.py", line 16, in 
    <module>
    from boto3.session import Session

but when i try to place this file(read.py) in lib folder then its import boto3 without an error why?

after place read.py file in lib

 Project 
 --> lib 
     --> boto3 package
     --> __init__.py
     --> read.py
 --> python script

标签: python-2.7python-import

解决方案


Add path in your PYTHONPATH

import os
path = os.path.dirname(os.path.realpath(__file__))

user_home = os.environ["HOME"]
os.environ["PYTHONPATH"] = path+'/lib'
import sys
sys.path.append( path+'/lib')
import boto3

推荐阅读