importing a package
Hi, I developed a package with a structure like this src/ tesfile.py dir1/ __init__.py file1.py dir2/ __init__.py file2.py The testfile is meant to try the code out that is specified in file1.py and file2.py Now i have another project where i want to use the code from that package. The structure of that project: src/ file3.py dir3/ __init__.py file4.py To use it, i copied the package in the root of the project: src/ file3.py dir3/ __init__.py file4.py package/ __init__.py dir1/ __init__.py file1.py dir2/ __init__.py file2.py In my code (file3.py) i then do an "import package.dir1.file1 as myobj" and access a class like this: myobj.LMyClass() (where myobj and LMyClass are decent names. Used these as example) That works but i get an error in the package files. I then get an error in package/dir1/file1.py on an import statement specified in that file1.py that says import dir2.file2 How come this works as standalone project and not when i try to use it as in situation 2 it doesn't seem to find module file2.py ? How can i solve that? Thanks Benedict -- http://mail.python.org/mailman/listinfo/python-list
Re: importing a package
> I developed a package with a structure like this > src/ > tesfile.py > dir1/ > __init__.py > file1.py > dir2/ > __init__.py > file2.py Importing dir2/file2 from dir1/file1.py works here, because when yuo started the testfile script the src/ directory was added to the sys.path list. If you relocate dir1/ and dir2/ in a "package" directory here it will not work. -- damjan -- http://mail.python.org/mailman/listinfo/python-list
Re: importing a package
Damjan wrote: >>I developed a package with a structure like this >>src/ >>tesfile.py >>dir1/ >>__init__.py >>file1.py >>dir2/ >>__init__.py >>file2.py > > > Importing dir2/file2 from dir1/file1.py works here, because when yuo started > the testfile script the src/ directory was added to the sys.path list. > > If you relocate dir1/ and dir2/ in a "package" directory here it will not > work. > > Indeed, when i do this, then it works import sys sys.path.append('package') However, why is it that package isn't added automatically to the pad? Regards, Benedict -- http://mail.python.org/mailman/listinfo/python-list
Re: importing a package
> Indeed, when i do this, then it works > import sys > sys.path.append('package') > > However, why is it that package isn't added automatically to the pad? When you execute a python program the directory where the program is is automatically added to sys.path. No other directory is added to the default builtin sys.path. In you case (the second case), you can import package.dir2.file2. -- damjan -- http://mail.python.org/mailman/listinfo/python-list
Re: importing a package
Damjan wrote: >>Indeed, when i do this, then it works >>import sys >>sys.path.append('package') >> >>However, why is it that package isn't added automatically to the pad? > > > When you execute a python program the directory where the program is is > automatically added to sys.path. No other directory is added to the default > builtin sys.path. > > In you case (the second case), you can import package.dir2.file2. OK, thanks for the info Regards, Benedict -- Benedict Verheyen Debian User http://www.heimdallit.bePublic Key 0x712CBB8D -- http://mail.python.org/mailman/listinfo/python-list
Problem with importing a package
I have to work with matrices on python and for that i need a package NUMPY. I downloaded it and saved it in the same folder as the program which imports it. But on running the program it gives an error "ImportError: No module named numpy". Do we need to save the file required to be imported in a specific folder? Plz help me out with this. Thanks Amit -- http://mail.python.org/mailman/listinfo/python-list
question about importing a package
I have a directory structure that looks like this: sample.py sub_one/ __init__.py # defines only the list__all__ = ['foo', 'bar'] foo.py # defines the function in_foo() bar.py # defines the function in_bar() In sample.py, I have this command at the top: from sub_one import * I can't refer to in_foo() and in_bar() without prefacing them with the module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to import them in the __main__ namespace of sample.py and refer to them as just in_foo() and in_bar(). I know this is frowned upon, but for my purposes it is the best choice. I have about 30 modules in my package (foos and bars) and I don't want 30 lines at the top of each file that uses this package. What am I doing wrong? Thanks, Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with importing a package
[EMAIL PROTECTED] wrote: > I have to work with matrices on python and for that i need a package > NUMPY. I downloaded it and saved it in the same folder as the program > which imports it. But on running the program it gives an error > "ImportError: No module named numpy". Do we need to save the file > required to be imported in a specific folder? did you follow the installation instructions supplied with that library? -- http://mail.python.org/mailman/listinfo/python-list
Re: question about importing a package
On Thu, Dec 6, 2012 at 3:58 PM, Matt wrote: > I have about 30 modules in my package (foos and bars) and I don't want 30 > lines at the top of each file that uses this package. What am I doing wrong? Not necessarily wrong, but definitely something to query: WHY do you have thirty modules in your package? How big are your source files - could you simply merge them into a single module? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: question about importing a package
On 6 Dec, 14:58, Matt wrote: > I have a directory structure that looks like this: > > sample.py > sub_one/ > __init__.py # defines only the list __all__ = ['foo', 'bar'] > foo.py # defines the function in_foo() > bar.py # defines the function in_bar() > > In sample.py, I have this command at the top: > > from sub_one import * > > What am I doing wrong? The statement `from sub_one import *` imports from sub_one/ __init__.py, which only imports the two modules into its namespace, not their contents. What you need to do is bring into __init__.py everything you want the star-import to pull into your code: __init__.py: from foo import * from bar import * foo.py: __all__ = [ 'in_foo' ] def in_foo(): ... bar.py: __all__ = [ 'in_bar' ] def in_bar(): ... If you structure is like this, you can restrict which items can be imported within the defining file. If it doesn't make sense to do it there, remove __all__ and just import directly in the __init__. -- http://mail.python.org/mailman/listinfo/python-list
Re: question about importing a package
On Wed, 05 Dec 2012 20:58:46 -0800, Matt wrote: > I have a directory structure that looks like this: > > sample.py > sub_one/ > __init__.py # defines only the list __all__ = ['foo', 'bar'] > foo.py # defines the function in_foo() > bar.py # defines the function in_bar() > > In sample.py, I have this command at the top: > > from sub_one import * > > I can't refer to in_foo() and in_bar() without prefacing them with the > module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to > import them in the __main__ namespace of sample.py and refer to them as > just in_foo() and in_bar(). Module `sub_one` has two public names, "foo" and "bar", exactly as you say. So when you import * from it, you only get two names. Now, you could do any of these inside sample.py: # 1 from sub_one.foo import in_foo from sub_one.bar import in_bar # 2 from sub_one import * in_foo = foo.in_foo in_bar = bar.in_foo Or you could turn to sub_one.__init__ and do this: # 3 __all__ = ['in_foo', 'in_bar'] from foo import in_foo from bar import in_bar or any combination of the above. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: question about importing a package
It works now. Steven and Alex, thanks for your help! I ended up leaving sample.py and foo.py and bar.p the way they were, and in __init__.py putting: from foo import * from bar import * So my mistake was not importing the foo and bar modules into sub_one/__init__.py. I also see how the __all__ array helps me control what gets imported. I can leave it out of __init__.py, and everything gets imported. So my three lessons are: 1) "from X import *" will look for an __all__ list in module X, or in __init__.py if X is a package instead of a module, and import only what is in that list. Module names are different than function names in that list. 2) if __all__ is not defined, "from X import *' will import everything in X's namespace 3) __init__.py acts like just another module, so you have to import the package contents that you want into it before you import the package into your code Thanks again for the help! -- http://mail.python.org/mailman/listinfo/python-list
Re: question about importing a package
On 12/6/2012 11:50 AM, Matt wrote: It works now. Steven and Alex, thanks for your help! I ended up leaving sample.py and foo.py and bar.p the way they were, and in __init__.py putting: from foo import * from bar import * So my mistake was not importing the foo and bar modules into sub_one/__init__.py. I also see how the __all__ array helps me control what gets imported. I can leave it out of __init__.py, and everything gets imported. So my three lessons are: 1) "from X import *" will look for an __all__ list in module X, or in __init__.py if X is a package instead of a module, and import only what is in that list. Module names are different than function names in that list. 2) if __all__ is not defined, "from X import *' will import everything in X's namespace ... that does not have a leading underscore. This is why there are things like import sys as _sys from itertools import chain as _chain in the stdlib when the module author does not define __all__. 3) __init__.py acts like just another module, so you have to import the package contents that you want into it before you import the package into your code -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
py2exe importing a package not in library.zip
Hi, I'm sorry if this has been asked before, but I can't seem to find an answer to this anywhere and so far, trial and error hasn't gotten me far either. Using python 2.4, I've created a testing application. When the app starts up, I do a [code]from tests import *[/code] and it looks at the /tests directory and loads up all the tests it finds at that time. This works fine when interpreting the python code directly. The problem I'm having is trying to create an executable for distribution. What I'd like to be able to do is to include everything in the library.zip *except* the tests directory and then as I (or whoever) creates more and more tests, have the ability to simply drop them into the directory and the next time the app starts, it'll pick them up. However, if I don't include the tests in library.zip, then when I hit the [code]from tests import *[/code], it complains "AttributeError: 'module' object has no attribute 'test1'" when it gets to the first test. And If I do include the tests in the library.zip, it works with the tests that I have now, but then I still can't add any new ones without the attribute error unless I regenerate the exe every time. Basically, it seems like it comes down to importing a package outside the library.zip. So, is there any way to do this with py2exe? Or even another exe creating application? Thanks, MyPetSlug -- http://mail.python.org/mailman/listinfo/python-list
Re: py2exe importing a package not in library.zip
On Aug 20, 4:48 pm, [EMAIL PROTECTED] wrote: > Hi, > I'm sorry if this has been asked before, but I can't seem to find an > answer to this anywhere and so far, trial and error hasn't gotten me > far either. > > Using python 2.4, I've created a testing application. When the app > starts up, I do a [code]from tests import *[/code] and it looks at > the /tests directory and loads up all the tests it finds at that > time. This works fine when interpreting the python code directly. > The problem I'm having is trying to create an executable for > distribution. > > What I'd like to be able to do is to include everything in the > library.zip *except* the tests directory and then as I (or whoever) > creates more and more tests, have the ability to simply drop them into > the directory and the next time the app starts, it'll pick them up. > However, if I don't include the tests in library.zip, then when I hit > the [code]from tests import *[/code], it complains "AttributeError: > 'module' object has no attribute 'test1'" when it gets to the first > test. And If I do include the tests in the library.zip, it works with > the tests that I have now, but then I still can't add any new ones > without the attribute error unless I regenerate the exe every time. > > Basically, it seems like it comes down to importing a package outside > the library.zip. So, is there any way to do this with py2exe? Or > even another exe creating application? > > Thanks,MyPetSlug Hi Again, So, someone responded with some tips about paths, so my paths are correct in the exe and I verified this by printing them out. So, my problem is not that, I guess. And in my original email, I simplified my scenario because I thought it was a problem with paths. So, let me back up a little. My tests directory actually has several sub-directories, so it's laid out like this: tests/testCategory1, tests/testCategory2, tests/ testCategory1/subCategory1/test1, and so on. With potentially hundreds of tests, I needed some organization. Anyway, in my tests directory, I have an __init__.py with " __all__=["testCategory1", "testCategory2"]" and it's this that the exe keeps choking on. Even though I append the path directly above the __all__ via " sys.path.append(os.path.abspath('') + '\\tests')" and "sys.path.append(os.path.abspath('') + '\\tests\\testCategory1')" (the second one just to be safe), when I execute the import *, it still tells me "AttributeError: 'module' object has no attribute 'testCategory1'" Again, this works fine when just executing the python code without the exe. What am I doing wrong? Or can anyone even point me to the right place to possibly find an answer? Thanks, MyPetSlug -- http://mail.python.org/mailman/listinfo/python-list