473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

__init__.py, __path__ and packaging


Hi everybody,

I'm trying to fix the packaging of a very simple package, but some problem
show me that I have not well understood the whole mechanism

The structure of my package (some debug functions) is as follows:

python/
`-- dbg/
|-- __init__.py
`-- lib
|-- __init__.py
|-- debug.py
`-- gtk_dbg.py
my sys.path includes 'python' and I wanted that the content of debug.py was
simply included by: 'import dbg', so I wrote dbg/__init__.py as follows:

import os
Dir = os.path.dirname(__file__)
__path__ = [os.path.join(Dir, 'lib')]
from debug import *

It seems to work:

python$ python -c 'import dbg; print dir(dbg)'
['DBG', 'Dir', '__builtins__', '__doc__', '__file__', '__name__', \
'__path__', 'caller', 'debug', 'dshow', 'os', 're', 'show_caller', \
'sql_debug', 'sys']

BUT, if I set some variables they are not correctly seen:

import dbg
dbg.DBG = 1
function test included in debug.py raises NameError:

def test():
print DBG

NameError: global name 'DBG' is not defined`

What's happening? DBG seems to be set, as shown by dir(dbg)... any hints?
I'd also accept a hint for a different approch, if it's the case, but I'd
really would also understant this issue

Thanks in advance
sandro
*:-)

--
Sandro Dentella *:-)
http://www.tksql.org TkSQL Home page - My GPL work
May 3 '06 #1
3 5777
Sandro Dentella wrote:
The structure of my package:

python/
`-- dbg/
|-- __init__.py
`-- lib
|-- __init__.py
|-- debug.py
`-- gtk_dbg.py

my sys.path includes 'python' and I wanted that the content of debug.py was
simply included by: 'import dbg', so I wrote dbg/__init__.py as follows:

import os
Dir = os.path.dirname(__file__)
__path__ = [os.path.join(Dir, 'lib')]
from debug import *
What you probably want in python/dbg/__init__.py to get values is:

from dbg.lib.debug import *
BUT, if I set some variables they are not correctly seen:
import dbg
dbg.DBG = 1
function test included in debug.py raises NameError:
def test():
print DBG
NameError: global name 'DBG' is not defined`

What's happening? DBG seems to be set, as shown by dir(dbg)... any hints?

You misunderstand modules and python variables. Each module has a
dictionary associating the names of its globals and their current
values. After:
import dbg.lib.debug, dbg.lib.gtk_dbg
you have four modules:
dbg # Corresponds to python/dbg/__init__.py
dbg.lib # Corresponds to python/dbg/lib/__init__.py
dbg.lib.debug # Corresponds to python/dbg/lib/debug.py
dbg.lib.gtk_dbg # Corresponds to python/dbg/lib/gtk_dbg.py
Each has its own globals.
after:
dbg.DBG = 1
the dbg module's global dictionary contains an entry mapping 'DBG' to 1
after:
dbg.DBG = 1+2
the dbg module's global dictionary contains an entry mapping 'DBG' to 3

In no case will an assignment to a global in dbg cause an assignment to
anything in dbg.lib.debug. The "from dbg.lib.debug import *" statement
can be seen as a module import followed by a fancy multiple assignment,
where module dbg.lib.debug is first imported, then its globals are
assigned to globals of the same names in module dbg.

--Scott David Daniels
sc***********@acm.org
May 3 '06 #2
In comp.lang.python, hai scritto:
Sandro Dentella wrote:
The structure of my package:

python/
`-- dbg/
|-- __init__.py
`-- lib
|-- __init__.py
|-- debug.py
`-- gtk_dbg.py

my sys.path includes 'python' and I wanted that the content of debug.py was
simply included by: 'import dbg', so I wrote dbg/__init__.py as follows:

import os
Dir = os.path.dirname(__file__)
__path__ = [os.path.join(Dir, 'lib')]
from debug import *
What you probably want in python/dbg/__init__.py to get values is:

from dbg.lib.debug import *


This does not work:

Traceback (most recent call last):
File "<string>", line 1, in ?
File "dbg/__init__.py", line 8, in ?
from dbg.lib.debug import *
ImportError: No module named lib.debug
BUT, if I set some variables they are not correctly seen:
import dbg
dbg.DBG = 1
function test included in debug.py raises NameError:
def test():
print DBG
NameError: global name 'DBG' is not defined`

What's happening? DBG seems to be set, as shown by dir(dbg)... any hints? You misunderstand modules and python variables. Each module has a
dictionary associating the names of its globals and their current
values. After:
import dbg.lib.debug, dbg.lib.gtk_dbg
you have four modules:
dbg # Corresponds to python/dbg/__init__.py
dbg.lib # Corresponds to python/dbg/lib/__init__.py
dbg.lib.debug # Corresponds to python/dbg/lib/debug.py
dbg.lib.gtk_dbg # Corresponds to python/dbg/lib/gtk_dbg.py
Each has its own globals.
after:
dbg.DBG = 1
the dbg module's global dictionary contains an entry mapping 'DBG' to 1
after:
dbg.DBG = 1+2
the dbg module's global dictionary contains an entry mapping 'DBG' to 3

In no case will an assignment to a global in dbg cause an assignment to
anything in dbg.lib.debug. The "from dbg.lib.debug import *" statement
can be seen as a module import followed by a fancy multiple assignment,
where module dbg.lib.debug is first imported, then its globals are
assigned to globals of the same names in module dbg.


This confirms to me that I'm seriously confused... so I started with a very
simple setup:
$ cat dbg.py
DBG = 1
def test():
global DBG
print DBG

def set():
global DBG
DBG = 3
$ cat m.py
from dbg import *

test()
#dbg.DBG = 2 ## does not work, no way to assign in module dbg
set() # this acts in dbg module and sets 'DBG = 3'
test() # test the value of DBG
print DBG
$ python m.py
1
3 # changed by 'set' that was 'imported'
1 # value of local DBG

isn't this contraddicting you words:
can be seen as a module import followed by a fancy multiple assignment,
where module dbg.lib.debug is first imported, then its globals are
assigned to globals of the same names in module dbg.


So: which is the way I can change a value of a package 'imported', only with
a function that sets it? is there a way to assign the value directly?
is there any way to make some introspection of what is really there (in
dbg)?

Thanks angain for any possible hint.

sandro
*:-)

--
Sandro Dentella *:-)
http://www.tksql.org TkSQL Home page - My GPL work
May 4 '06 #3
> Now, why you couldn't do "dbg.DBG = ..."? Very simple... "from
module import *" doesn't give you a dbg /module/, it only gives you
references to each piece inside the module.
really the reason why I wanted that should probably be solved in other
ways. I just wanted to split my dbg module in different files but load the
dbg module in one single operation:

dbg/
|-- __init__.py
|-- lib
|-- __init__.py
|-- debug.py
|-- gtk_dbg.py

and inside dbg/__init__.py I used "from dbg.debug import *" so that a single
'import dbg' could present me the module 'debug' (of course in this simple
case seems easier to put debug.py directly under dbg, but my main case is
a much more complex module, with a tree structure that reflects the
relation between modules that I want to hide to the end user).

But to summarize, if I use 'from my_module import *' there is no way to reach
directly 'my_module' and set a variable there?

Thanks again
sandro
*:-)
I'm trying to fix the packaging of a very simple package, but some problem
show me that I have not well understood

the structure of my package (some debug functions) is as follows:

python/
`-- dbg/
|-- __init__.py
`-- lib
|-- __init__.py
|-- debug.py
`-- gtk_dbg.py
my sys.path includes 'python' and I wanted that the content of debug.py was
simply included by: 'import dbg', so I wrote dbg/__init__.py as follows:

import os
Dir = os.path.dirname(__file__)
__path__ = [os.path.join(Dir, 'lib')]
from debug import *

--
Sandro Dentella *:-)
e-mail: sa****@e-den.it
http://www.tksql.org TkSQL Home page - My GPL work
May 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gary Wilson Jr | last post by:
I'm creating a python package foo. What is intended use for __init__.py files? Well, I found this: http://www.python.org/doc/essays/packages.html >From what I can gather it is for initialization...
1
by: Barry Edmund Wright | last post by:
Problem when using Xp Packaging Wizard and the database being able to find the Application Icon after database is deployed. Under Menu/Tools/Startup form I have: Application Icon:...
1
by: Coy Howe | last post by:
This one seems bizarre! We have a database consisting of a main table and 12 - 15 "sub" tables, which are connected via cascading relationships. The database performs many complex calculations...
2
by: Sky | last post by:
In PHP there is a way of finding your current location in the file structure with the __LINE__ and __PATH__ special commands. Comes in handy for debugging, as well as relative pathing when making...
3
by: Paul Aspinall | last post by:
Hi I want to package my C# winforms app, to be deployed with MSDE, as easily as possible for the end user. I want to create an MSI or Installshield (prefer MSI), to setup my C# app, together...
3
by: VJ | last post by:
I have a MSI that installs some Add-ins for office applications. This package was generated by the Add-in project Wizard. How do I include this project as part of the package I build for my...
0
by: blkwebman | last post by:
I'm trying to create a "standalone" install package (without any of the dialog boxes that a standard setup package would have). I think I understand how to do it in VB6 (using PDCmdLn.exe); I have...
4
by: Mr Seth T | last post by:
Hey, I have spent several days trying to find out how to do something, and i don't know if I am blind or what, but I can not find it. I am developing a web app and I need it to run an activex...
5
by: Paul Rubin | last post by:
I've been through this kind of thing a few times in the past and received excellent advice here on clpy about how to deal with specific technical aspects (e.g. how to use setuptools, InnoSetup,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.