473,287 Members | 1,880 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,287 software developers and data experts.

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
Jun 27 '08 #1
0 1867

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

Similar topics

1
by: Steve George | last post by:
Hi, I have a scenario where I have a master schema that defines a number of complex and simple types. I then have a number of other schemas (with different namespaces) where I would like to reuse...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
14
by: Mark B | last post by:
Our webhost (www.usbusinessweb.net) had a W2K IIS5 server crash after a scheduled hard-boot occurred during a ms-security patch install overnight. They couldn't get the server working again so they...
2
by: Carmit | last post by:
Hi, I'm trying to build a proxy for this webservice: http://webservices.sabre.com/wsdl/sabreXML1.0.00/tpf/EndTransactionLLSRQ.wsdl I'm getting the following error: Error: Unable to import...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
2
by: momo | last post by:
Hello Guys, I have a bit of a problem, I created a Dll called SecureQueryStringDll.dll and I had the dll put bin folder of my application first and it did not work so I then put it in the bin...
1
by: Alex Maghen | last post by:
I've been using my installed VS 2005 for several months with no problem. Suddenly, something strange is happeneing and I'm not sure if it's something I'm missing in ASP.NET or something that's...
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.