473,769 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

import question

Question:
my src path looks like this:
src\root\sub1
src\root\sub2

My main code is in root, lets say main.py and there is also a lib.py.
In sub1 there if foo1.py and sub2 foo2.py

Sorry for the long introduction... please don't stop reading... :-(

In both sub1 and sub2 there is an empty __init__.py for making them a package.
Ok the problem:

this works fine:

main.py
from sub1 import foo1
from sub1 import foo2

However, in foo1 and foo2 I have this line "import lib".

So lib.py is not in the same path as foo1.py and foo2.py, it seems to find lib.py in a higher path.
(Reason for this is the import from main , which has lib.py in the same path?)

However, if I'm in the subfolders sub1 or sub2, and I run foo1.py or foo2.py standalone, they can't find lib.py. Yeah of course.

So, how to avoid this? How can I refer back to a module higher in the "hierarchic al" path.
Up is so easy.. just say import foo.foo2.foo3. But up, what is the correct way of doing that?
Something to put in __init__.py, like sys.path.apppen d('..')? Looks ugly to me....

Or, the way how I share lib for both files is wrong, please let me know.
Anyway, I need this behavior because of my unittest structure.

Thanks,
Vincent

Jul 18 '05 #1
1 1658
> So, how to avoid this? How can I refer back to a module higher in the "hierarchic al" path.
Up is so easy.. just say import foo.foo2.foo3. But up, what is the correct way of doing that?
Something to put in __init__.py, like sys.path.apppen d('..')? Looks ugly to me....


PEP 328, Relative imports should take care of your issue.

In the meantime...

#in main.py
import lib

#in foo1.py and foo2.py
import sys
lib = sys.modules['lib']
Now both of them will refer to the same 'lib' that main.py imported.

- Josiah
Jul 18 '05 #2

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

Similar topics

1
1630
by: Kevin MacKenzie | last post by:
I'm a complete newbie to using Python. I have a small question about importing modules. Is there any difference between the two following statements, and what (if any) are they? >>> from Module import * and
0
6913
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
3
6272
by: Doug Baroter | last post by:
Hi, One of my clients has the following situation. They use Access DB for data update etc. some business functions while they also want to view the Access data quickly and more efficiently in SQL Server 2000. Huge Access db with over 100 user tables, over 60 MB data. The DTS package that comes with SQL Server 2000 seems pretty "messy" in the sense that it assumes that one needs to do one time import only or accurately it does not...
2
22713
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql format. I'm trying to figure out how to import that into SQL Server 2000 so that I'll be able to manipulate it in a gui format, rather than command line. I can't find any import that takes a .sql file. I've been trying to load it into the query...
2
10029
by: Charles Fineman | last post by:
I've been asked to look over an integration toolkit that has a bunch of schemas to specify message format. There are a couple of strange things I noticed right off the bat and I wanted to get others' thoughts on it (disclaimer, obviously I'm no XML expert so forgive me if these are basic). --------Exhibit A: The start of a response message schema----------- <xsd:schema targetNamespace="http://www.QQQ.com"
1
6703
by: Dan | last post by:
Could someone please help me with auto importing a series of data files into an Access table. I tried to follow code given below in a previous messagebut i'm getting error messages. Here's my database stats: Path: C:\Database (contains the database and all the text files to be imported) Text files to import: (SampleData4.txt and SampleData3.txt as testing examples)
4
6110
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: ---------------------------------------------------------------------------------------------------------------------------------------------------------- The @ Import Directive Next to @ Page, the directive that ASP.NET programmers use the most is @ Import. The @ Import directive is ASP.NET's...
3
12300
by: SMALLp | last post by:
Hy! I'm new in Linux, and i feel little less newer in python. I need advice and help. I'm making an application witch purpose is irrelevant. It has a lot of code for now and I've only made interface. So I've tried to split code into separate files and in windows as I remember worked file when i wrote eg. import myFile but now in Ubuntu it says Module not found. (I'm using Ubuntu and I've installed python 2.5 and wxPython 2.8.4, and I'm...
0
1228
by: 123bargains | last post by:
Hello, I have a question on importing a database from MSSQL 2000 to MSSQL 2005. I hope someone on here can help me answer it. I am trying to import a database from MSSQL 2000 to 2005. But, when I tried to import it, the data and tables came in just fine, but the properties such as Identity Values, Column Defaults, Indexes did not import. Also the stored procedures did not import. This causes a problem as I have a lot of tables and I have...
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.