Creating Zip file like java jar file

Z

zaheer.agadi

Hi,
I want to create zip file equivalent to java jar file,I created a zip
file of my sources and added some __main__.py
some how I am getting an error saying __main.py does not exist in the
zip file

Copyproject(main folder)
|
|_____src(folder)
| |
| |__Nertworkpackage
| | |
| | |__Storepackage
| | |
| |__FtpPackage |__module1.py
| |__module2.py
|____ tests |__ module3.py

Copyproject(main folder) | |_____src(folder) | | | |__Nertworkpackage
| | | | | |__Storepackage | | | | |__FtpPackage |__module1.py | |
__module2.py |____ tests |__ module3.py

Module1 takes some commandline parameters that will do some operation
Like If I say
" module1.py --uploadFile <file name> " A file should get uploaded
(this works)
Now I want to do
Copyproject.zip --uploadFile <filename> to upload the file

How should I do this, I tried to put __main__ parallel to src folder
it says __Main__.py not found in Copyproject.zip..?
 
G

Gabriel Genellina

I want to create zip file equivalent to java jar file,I created a zip
file of my sources and added some __main__.py
it says __Main__.py not found in Copyproject.zip..?

__main__.py must exist in the root directory.
 
Z

zaheer.agadi

__main__.py must exist in the root directory.

What do you mean by root directory..?What is this directory in
Windows..?You mean the top level folder of the project?in my case
copyproject folder..?
 
L

Lie Ryan

What do you mean by root directory..?What is this directory in
Windows..?You mean the top level folder of the project?in my case
copyproject folder..?

root directory is the topmost directory (imagine a tree, the root is
where all the branches branched from), in this case the root directory
is your copyproject main folder.
 
L

Lie Ryan

What do you mean by root directory..?What is this directory in
Windows..?You mean the top level folder of the project?in my case
copyproject folder..?

root directory is the topmost directory (imagine a tree, the root is
where all the branches branched from), in this case the root directory
is your copyproject main folder.
 
Z

zaheer.agadi

root directory is the topmost directory (imagine a tree, the root is
where all the branches branched from), in this case the root directory
is your copyproject main folder.



I wonder, I do have the __main__.py in the root directory,but still
getting
python.exe cannot find __main__.py in CopyProject.zip

I used the following command
python Copyproject.zip -m __main__.py --uploadFile and also tried with
python Copyproject.zip --uploadFile
both give me the same error , I can see the sys.path contains the
project folder.
 
G

Gabriel Genellina

I wonder, I do have the __main__.py in the root directory,but still
getting
python.exe cannot find __main__.py in CopyProject.zip

The top of the tree inside the zip file. Depth zero. Not inside any
directory. Above anything else. Just a bare name.

Open your zip using the zipfile module:

import zipfile
z = zipfile.ZipFile("xxx.zip")
z.printdir()

You should see a line starting with __main__.py *without* any / in it.
I used the following command
python Copyproject.zip -m __main__.py --uploadFile and also tried with
python Copyproject.zip --uploadFile

Use the second one.
both give me the same error , I can see the sys.path contains the
project folder.

Uh? Which project folder? Isn't it in the .zip?
 
R

rdmurray

I wonder, I do have the __main__.py in the root directory,but still getting
python.exe cannot find __main__.py in CopyProject.zip

I used the following command
python Copyproject.zip -m __main__.py --uploadFile and also tried with
python Copyproject.zip --uploadFile
both give me the same error , I can see the sys.path contains the
project folder.

How did you create the zip? The __main__.py file has to be at the
_top level_ of the zip file. In other words:

zip test.zip __main.py othermodule.py somedir

works but

zip test.zip myproject

does not.

--RDM
 
Z

zaheer.agadi

En Sat, 28 Feb 2009 16:51:04 -0200, <[email protected]> escribió:





The top of the tree inside the zip file. Depth zero. Not inside any
directory. Above anything else. Just a bare name.

Open your zip using the zipfile module:

import zipfile
z = zipfile.ZipFile("xxx.zip")
z.printdir()

You should see a line starting with __main__.py *without* any / in it.


Use the second one.
Uh? Which project folder? Isn't it in the .zip?
Yeah I meant zip file.
I can get to work but is it is not able to locate the packages,says
import error cant find the package and module

here is my code

import sys
import os.path as op
sys.path.insert(0, op.join(op.dirname(op.abspath(__file__)),
"somezip.zip"))

import zipfile
z = zipfile.ZipFile("somezip.zip")
z.printdir()

import some.storagepackage.somemodule
print "upload"
print "syspath",sys.path

#do something

should it not find the modules when the zip files is in sys.path..?
 
S

Steve Holden

Yeah I meant zip file.
I can get to work but is it is not able to locate the packages,says
import error cant find the package and module

here is my code

import sys
import os.path as op
sys.path.insert(0, op.join(op.dirname(op.abspath(__file__)),
"somezip.zip"))

import zipfile
z = zipfile.ZipFile("somezip.zip")
z.printdir()

import some.storagepackage.somemodule
print "upload"
print "syspath",sys.path

#do something

should it not find the modules when the zip files is in sys.path..?

For this to work not only should your zipfile contain

some/storagepackage/somemodule.py, but some and some/storagepackage must
both contain __init__.py files to be recognized as packages.

Have you tried getting your imports working from the file store and then
zipping up what works afterwards?

regards
Steve
 
G

Gabriel Genellina

I can get to work but is it is not able to locate the packages,says
import error cant find the package and module

Make it work *before* you attempt to zip the files. A complete session:

C:\TEMP>tree /a /f test_main_in_zip
Listado de rutas de carpetas para el volumen Cucho
El número de serie del volumen es 007A005F F4EC:16A9
C:\TEMP\TEST_MAIN_IN_ZIP
| __main__.py
|
\---pkgA
foo.py
__init__.py


C:\TEMP>cd test_main_in_zip

C:\TEMP\test_main_in_zip>type __main__.py
#!/bin/env python

"""This script is the entry point
to the application"""

import sys
import pkgA.foo

def main():
print "I'm main():"
print "__name__", __name__
print "__file__", __file__
print "sys.path", sys.path[:3], "..."
print
pkgA.foo.bar()

main()

C:\TEMP\test_main_in_zip>type pkgA\__init__.py
print "I'm __init__.py"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>type pkgA\foo.py
def bar():
print "I'm bar() inside foo.py:"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>python __main__.py
I'm __init__.py
__name__ pkgA
__file__ C:\TEMP\test_main_in_zip\pkgA\__init__.py

I'm main():
__name__ __main__
__file__ __main__.py
sys.path ['C:\\TEMP\\test_main_in_zip',
'c:\\apps\\python26\\python26.zip', 'c:\
\apps\\python26\\DLLs'] ...

I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ C:\TEMP\test_main_in_zip\pkgA\foo.py


C:\TEMP\test_main_in_zip>zip anyname.zip __main__.py pkgA\*
adding: __main__.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.pyc (196 bytes security) (deflated 41%)
adding: pkgA/__init__.py (196 bytes security) (deflated 38%)
adding: pkgA/__init__.pyc (196 bytes security) (deflated 29%)

C:\TEMP\test_main_in_zip>python anyname.zip
I'm __init__.py
__name__ pkgA
__file__ anyname.zip\pkgA\__init__.pyc

I'm main():
__name__ __main__
__file__ None
sys.path ['anyname.zip', 'c:\\apps\\python26\\python26.zip',
'c:\\apps\\python26
\\DLLs'] ...

I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ anyname.zip\pkgA\foo.pyc
 
Z

zaheer.agadi

En Sun, 01 Mar 2009 03:16:53 -0200, <[email protected]> escribió:


I can get to work but is it is not able to locate the packages,says
import error cant find the package and module

Make it work *before* you attempt to zip the files. A complete session:

C:\TEMP>tree /a /f test_main_in_zip
Listado de rutas de carpetas para el volumen Cucho
El número de serie del volumen es 007A005F F4EC:16A9
C:\TEMP\TEST_MAIN_IN_ZIP
| __main__.py
|
\---pkgA
foo.py
__init__.py

C:\TEMP>cd test_main_in_zip

C:\TEMP\test_main_in_zip>type __main__.py
#!/bin/env python

"""This script is the entry point
to the application"""

import sys
import pkgA.foo

def main():
print "I'm main():"
print "__name__", __name__
print "__file__", __file__
print "sys.path", sys.path[:3], "..."
print
pkgA.foo.bar()

main()

C:\TEMP\test_main_in_zip>type pkgA\__init__.py
print "I'm __init__.py"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>type pkgA\foo.py
def bar():
print "I'm bar() inside foo.py:"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>python __main__.py
I'm __init__.py
__name__ pkgA
__file__ C:\TEMP\test_main_in_zip\pkgA\__init__.py

I'm main():
__name__ __main__
__file__ __main__.py
sys.path ['C:\\TEMP\\test_main_in_zip',
'c:\\apps\\python26\\python26.zip', 'c:\
\apps\\python26\\DLLs'] ...

I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ C:\TEMP\test_main_in_zip\pkgA\foo.py

C:\TEMP\test_main_in_zip>zip anyname.zip __main__.py pkgA\*
adding: __main__.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.pyc (196 bytes security) (deflated 41%)
adding: pkgA/__init__.py (196 bytes security) (deflated 38%)
adding: pkgA/__init__.pyc (196 bytes security) (deflated 29%)

C:\TEMP\test_main_in_zip>python anyname.zip
I'm __init__.py
__name__ pkgA
__file__ anyname.zip\pkgA\__init__.pyc

I'm main():
__name__ __main__
__file__ None
sys.path ['anyname.zip', 'c:\\apps\\python26\\python26.zip',
'c:\\apps\\python26
\\DLLs'] ...

I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ anyname.zip\pkgA\foo.pyc
Make it work *before* you attempt to zip the files.
Thanks a lot Gabriel, yes this works fine when I am running it
outside of zip.
when I say python __main__.py --uploadfile it works fine
I dont know what happens when I zip it.

And Steve: all of the packages have __init__.py inside them, I have
posted the tree structure of application if it helps.

C:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\PYTHNSTUF
\TestApplication
├───src
│ ├───network
│ │ ├───.svn
│ │ │ └───text-base
│ │ └───storage
│ │ └───.svn
│ │ └───text-base
│ ├───uc
│ │ └───some
│ │ └───extra
│ │ └───package
│ └───webdav
│ └───acp
└───test
└───.svn
└───text-base
 
Z

zaheer.agadi

En Sun, 01 Mar 2009 03:16:53 -0200, <[email protected]> escribió:
Make it work *before* you attempt to zip the files. A complete session:
C:\TEMP>tree /a /f test_main_in_zip
Listado de rutas de carpetas para el volumen Cucho
El número de serie del volumen es 007A005F F4EC:16A9
C:\TEMP\TEST_MAIN_IN_ZIP
| __main__.py
|
\---pkgA
foo.py
__init__.py
C:\TEMP>cd test_main_in_zip
C:\TEMP\test_main_in_zip>type __main__.py
#!/bin/env python
"""This script is the entry point
to the application"""
import sys
import pkgA.foo
def main():
print "I'm main():"
print "__name__", __name__
print "__file__", __file__
print "sys.path", sys.path[:3], "..."
print
pkgA.foo.bar()

C:\TEMP\test_main_in_zip>type pkgA\__init__.py
print "I'm __init__.py"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>type pkgA\foo.py
def bar():
print "I'm bar() inside foo.py:"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>python __main__.py
I'm __init__.py
__name__ pkgA
__file__ C:\TEMP\test_main_in_zip\pkgA\__init__.py
I'm main():
__name__ __main__
__file__ __main__.py
sys.path ['C:\\TEMP\\test_main_in_zip',
'c:\\apps\\python26\\python26.zip', 'c:\
\apps\\python26\\DLLs'] ...
I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ C:\TEMP\test_main_in_zip\pkgA\foo.py
C:\TEMP\test_main_in_zip>zip anyname.zip __main__.py pkgA\*
adding: __main__.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.pyc (196 bytes security) (deflated 41%)
adding: pkgA/__init__.py (196 bytes security) (deflated 38%)
adding: pkgA/__init__.pyc (196 bytes security) (deflated 29%)
C:\TEMP\test_main_in_zip>python anyname.zip
I'm __init__.py
__name__ pkgA
__file__ anyname.zip\pkgA\__init__.pyc
I'm main():
__name__ __main__
__file__ None
sys.path ['anyname.zip', 'c:\\apps\\python26\\python26.zip',
'c:\\apps\\python26
\\DLLs'] ...
I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ anyname.zip\pkgA\foo.pyc

Thanks a lot Gabriel, yes this works fine when I am running it
outside of zip.
when I say python __main__.py --uploadfile it works fine
I dont know what happens when I zip it.

And Steve: all of the packages have __init__.py inside them, I have
posted the tree structure of application if it helps.

C:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\PYTHNSTUF
\TestApplication
├───src
│ ├───network
│ │ ├───.svn
│ │ │ └───text-base
│ │ └───storage
│ │ └───.svn
│ │ └───text-base
│ ├───uc
│ │ └───some
│ │ └───extra
│ │ └───package
│ └───webdav
│ └───acp
└───test
└───.svn
└───text-base

and also what it gives me when I Do, > python BRU.zip
some list of files
:
:
BRU/__init__.py 2009-02-28
17:08:10 1
BRU/__main__.py 2009-03-01
18:17:20 8
BRU/ 2009-03-01 14:52:58
Traceback (most recent call last):
File "C:\Python26\lib\runpy.py", line 121, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python26\lib\runpy.py", line 34, in _run_code
exec code in run_globals
File "BRU.zip\__main__.py", line 18, in <module>
ImportError: No module named network.storage

I think I should I also mention I create this application using
ecliplse plugin

I appreciate your help Gabriella thanks a lot,
 
Z

zaheer.agadi

En Sun, 01 Mar 2009 03:16:53 -0200, <[email protected]> escribió:
I want to create zip file equivalent to java jar file,I created a
zip
file of my sources and added some __main__.py
it says __Main__.py not found in Copyproject.zip..?
I can get to work but is it is not able to locate the packages,says
import error cant find the package and module
Make it work *before* you attempt to zip the files. A complete session:
C:\TEMP>tree /a /f test_main_in_zip
Listado de rutas de carpetas para el volumen Cucho
El número de serie del volumen es 007A005F F4EC:16A9
C:\TEMP\TEST_MAIN_IN_ZIP
| __main__.py
|
\---pkgA
foo.py
__init__.py
C:\TEMP>cd test_main_in_zip
C:\TEMP\test_main_in_zip>type __main__.py
#!/bin/env python
"""This script is the entry point
to the application"""
import sys
import pkgA.foo
def main():
print "I'm main():"
print "__name__", __name__
print "__file__", __file__
print "sys.path", sys.path[:3], "..."
print
pkgA.foo.bar()
main()
C:\TEMP\test_main_in_zip>type pkgA\__init__.py
print "I'm __init__.py"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>type pkgA\foo.py
def bar():
print "I'm bar() inside foo.py:"
print "__name__",__name__
print "__file__",__file__
print
C:\TEMP\test_main_in_zip>python __main__.py
I'm __init__.py
__name__ pkgA
__file__ C:\TEMP\test_main_in_zip\pkgA\__init__.py
I'm main():
__name__ __main__
__file__ __main__.py
sys.path ['C:\\TEMP\\test_main_in_zip',
'c:\\apps\\python26\\python26.zip', 'c:\
\apps\\python26\\DLLs'] ...
I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ C:\TEMP\test_main_in_zip\pkgA\foo.py
C:\TEMP\test_main_in_zip>zip anyname.zip __main__.py pkgA\*
adding: __main__.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.py (196 bytes security) (deflated 40%)
adding: pkgA/foo.pyc (196 bytes security) (deflated 41%)
adding: pkgA/__init__.py (196 bytes security) (deflated 38%)
adding: pkgA/__init__.pyc (196 bytes security) (deflated 29%)
C:\TEMP\test_main_in_zip>python anyname.zip
I'm __init__.py
__name__ pkgA
__file__ anyname.zip\pkgA\__init__.pyc
I'm main():
__name__ __main__
__file__ None
sys.path ['anyname.zip', 'c:\\apps\\python26\\python26.zip',
'c:\\apps\\python26
\\DLLs'] ...
I'm bar() inside foo.py:
__name__ pkgA.foo
__file__ anyname.zip\pkgA\foo.pyc
Thanks a lot Gabriel, yes this works fine when I am running it
outside of zip.
when I say python __main__.py --uploadfile it works fine
I dont know what happens when I zip it.
And Steve: all of the packages have __init__.py inside them, I have
posted the tree structure of application if it helps.
C:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\PYTHNSTUF
\TestApplication
├───src
│ ├───network
│ │ ├───.svn
│ │ │ └───text-base
│ │ └───storage
│ │ └───.svn
│ │ └───text-base
│ ├───uc
│ │ └───some
│ │ └───extra
│ │ └───package
│ └───webdav
│ └───acp
└───test
└───.svn
└───text-base

and also what it gives me when I Do, > python BRU.zip
some list of files
:
:
BRU/__init__.py 2009-02-28
17:08:10 1
BRU/__main__.py 2009-03-01
18:17:20 8
BRU/ 2009-03-01 14:52:58
Traceback (most recent call last):
File "C:\Python26\lib\runpy.py", line 121, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python26\lib\runpy.py", line 34, in _run_code
exec code in run_globals
File "BRU.zip\__main__.py", line 18, in <module>
ImportError: No module named network.storage

I think I should I also mention I create this application using
ecliplse plugin

I appreciate your help Gabriella thanks a lot,

Thanks a lot folks it is solved now,the problem was I had to create a
zip file from the location where actual package declarations are there
and not from the top level directory whatever I had.
I created zip file from the src directory of the my tree which had all
the packages as its next level contents and imports were resolved.
 
G

Gabriel Genellina

and also what it gives me when I Do, > python BRU.zip
some list of files
:
:
BRU/__init__.py 2009-02-28
17:08:10 1
BRU/__main__.py 2009-03-01
18:17:20 8

So the __main__.py is recognized and runs. That's good news. But I see
*another* __main__.py listed that should not exist, in a directory BRU.
Are you sure you tested using the *outer* __main__.py? And what is that
__init__.py?

Test in *another* directory, not your development environment, and remove
all those extra artifacts like .svn directories and such that you're not
going to deploy, I presume.
Traceback (most recent call last):
File "C:\Python26\lib\runpy.py", line 121, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python26\lib\runpy.py", line 34, in _run_code
exec code in run_globals
File "BRU.zip\__main__.py", line 18, in <module>
ImportError: No module named network.storage

I see "network" inside "src" in your directory tree. For something like
"ipmort network.storage" to work, "src" must be in the import path used by
Python (sys.path).
I appreciate your help Gabriella thanks a lot,

My name is *Gabriel* and I'm male...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top