Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import a 'zip' file to my .py

Tags:

python

import

zip

when i use http://github.com/joshthecoder/tweepy-examples ,

i find :

import tweepy

in the appengine\oauth_example\handlers.py

but i can't find a tweepy file or tweepy's 'py' file, except a tweepy.zip file,

i don't think this is right,cauz i never import a zip file,

i find this in app.py:

import sys
sys.path.insert(0, 'tweepy.zip')

why ?

how to import a zip file..

thanks

updated

a.py :

import sys
sys.path.insert(0, 'b.zip')

import b
print b

b.zip:

b file
   |-----__init__.py
   |-----c.py

c.py:

cc='ccccc'

the error is :

> "D:\Python25\pythonw.exe"  "D:\zjm_code\a.py" 
Traceback (most recent call last):
  File "D:\zjm_code\a.py", line 9, in <module>
    import b
ImportError: No module named b

updated2

it is ok now ,

the error's reason is : i rename b.rar to b.zip

like image 878
zjm1126 Avatar asked May 22 '10 03:05

zjm1126


2 Answers

The name of the zip file is irrelevent when searching for modules - this allows you to include version numbers in the file name, such as my_b_package.1.2.3.zip.

To import from a zip file, you need to replicate the full package structure within it. In this case, you need a package b, with the __init__.py and c.py modules.

I.e:

b.zip
|
| -- b <dir>
     | -- __init__.py
     | -- c.py
like image 65
Simon Callan Avatar answered Sep 21 '22 17:09

Simon Callan


You don't import zip files, you add them to sys.path so that you can import modules within them. sys.path is a list, and as such the normal list methods/operations (e.g. .append()) all work on it.

like image 34
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 17:09

Ignacio Vazquez-Abrams