Re: problem with import / namespace



ohad frand wrote:
Hi
I have a problem that the solution to it must be very simple but i couldnt fint it's answer in the internet so far (i searched for couple of days)
the problme is as follows:

i have two directories e.g. "\\1" and "\\2"
in each directory i have two files with the same names e.g. "tmp1.py" and "tmp2.py"

So this is what you have:

/1/tmp1.py
/1/tmp2.py
/2/tmp1.py
/2/tmp2.py

each tmp2.py file imports tmp1 from its same directory (import tmp1) - thats the problem
if i execute one file (\\1\tmp2.py) than the execution is ok
but when i try after that to execute the second file (\\2\tmp2.py) than the tmp1 file from the wrong directory ( - directory 1 in this case) is imported instead.

When you try to import a module, python starts to search for it. The was it does the search is very well defined. It mostly depends on the current directory and sys.path. You can read more about this here:

http://docs.python.org/tut/node8.html#searchPath

This is very basic thing - you should read and go through the tutorial before asking questions like this. :-)

i tried many things to try to solve it, i removed the previous path from sys.path and added the new one, i tried to change current working directory with os.chdir()
I tried to delete from locals and from globals the name tmp1 before running the second file but nothing worked.
please help
The problem is still not well defined. Python works as expected and documented, but apparently you do not know how to import 1/tmp2.py from 2/tmp1.py. There are several ways to do it, and we cannot tell which one is correct. It depends on what are these modules for.

Here are the solution that you would use (most likely) as a beginner:


#1 first, rename your "1" and "2" directories to "one" and "two". If you are creating a package with modules, then you have to define the package's name with its directory. Since identifiers cannot begin with digits in Python, you need to use an identifier-like name for your subdirs. It is a good idea anyway. A package name called "1" would tell nothing about what it does?
#2 place your main application in /app/app.py
#3 create /app/one/__init__.py and /app/two/__init__.py files (they can be empty)
- inside your app.py file either make sure that the current dir is /app, or insert /app in the first place in sys.path


Then for example, inside two/tmp1.py you can do this:


import one.tmp1
import one.tmp2
import two.tmp1

one.tmp1.somefunc()
two.tmp1.somefunc()


You got the idea.

Laszlo


.



Relevant Pages

  • Re: Using Python in ActiveX task - works when run as separate step
    ... when executing the package. ... Peter Yang, if you can detail some of what you did to get Perl working, I'd ... >> was trying to use Python in this case because I know it a little better than ... >>>You could execute the step in DTS package window. ...
    (microsoft.public.sqlserver.dts)
  • RE: Using Python in ActiveX task - works when run as separate step, hangs when executing package
    ... The whole DTS package also works properly for perl. ... I could execute the task but it stoped to respond when execute the whole ... It seems Python cannot work with DTS in SQL server. ... Using Python in ActiveX task - works when run as separate step, ...
    (microsoft.public.sqlserver.dts)
  • RE: Using Python in ActiveX task - works when run as separate step
    ... was trying to use Python in this case because I know it a little better than ... > in ActiveX task, ... > You could execute the step in DTS package window. ...
    (microsoft.public.sqlserver.dts)
  • Re: Using Python in ActiveX task - works when run as separate step
    ... The way I defined my task was by defining a Mainfunction in Python and putting all of my code in there. ... Though it is possible to use different script engine such as perl, Python in ActiveX task, it is not official support. ... When I step through the package running each step, ... | saying that the function is starting to execute. ...
    (microsoft.public.sqlserver.dts)
  • Re: Finding installed package files
    ... for making installation information more easily accessible? ... >> somewhere where more information about the key parts of the package ... >> As an example I installed the latest Python on Fedora 3. ... what one would expect is a form of help documentation much more integrated ...
    (alt.os.linux.redhat)