473,574 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accesing modules in sibling packages

9 New Member
Hi,
I've create a file hierarchy:
test/
__init__.py
one/
__init__.py
one.py
two/
__init__.py
two.py


all the __init__.py files are empty.
the code in one.py is
Expand|Select|Wrap|Line Numbers
  1. import test.two
  2. print two.v
  3.  
the code in two.py is
Expand|Select|Wrap|Line Numbers
  1. v=0
  2.  

but when i run one.py i get an error:

Traceback (most recent call last):
File "C:\Python25\My Scripts\Test\1\ one.py", line 1, in <module>
import Test.two
ImportError: No module named Test.two
Sep 11 '07 #1
4 6181
bvdet
2,851 Recognized Expert Moderator Specialist
Hi,
I've create a file hierarchy:
test/
__init__.py
one/
__init__.py
one.py
two/
__init__.py
two.py


all the __init__.py files are empty.
the code in one.py is
Expand|Select|Wrap|Line Numbers
  1. import test.two
  2. print two.v
  3.  
the code in two.py is
Expand|Select|Wrap|Line Numbers
  1. v=0
  2.  

but when i run one.py i get an error:

Traceback (most recent call last):
File "C:\Python25\My Scripts\Test\1\ one.py", line 1, in <module>
import Test.two
ImportError: No module named Test.two
Try this:
Expand|Select|Wrap|Line Numbers
  1. import test.two.two
Sep 11 '07 #2
noama
9 New Member
Try this:
Expand|Select|Wrap|Line Numbers
  1. import test.two.two
Nope. still the same error.
Sep 12 '07 #3
elcron
43 New Member
try this:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two.two
  3. print two.two.v
  4.  
Though in the test/two/__init__.py I would add:
Expand|Select|Wrap|Line Numbers
  1. from two import *
  2.  
and change the code in one.py to:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two
  3. print two.v
  4.  
Sep 13 '07 #4
bartonc
6,596 Recognized Expert Expert
try this:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two.two
  3. print two.two.v
  4.  
Though in the test/two/__init__.py I would add:
Expand|Select|Wrap|Line Numbers
  1. from two import *
  2.  
and change the code in one.py to:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two
  3. print two.v
  4.  
Nice trick, my friend!
Oct 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1944
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is inside a package, the invocation will fail with a "Module not found" error. This PEP is aimed at fixing that :) Cheers, Nick.
15
2576
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that for Python 2.5. Previously, posting of a draft version of the PEP to python-dev and python-list didn't actually generate any responses. I'm not...
3
2274
by: Dennis Clark | last post by:
This is a total newb question, you have been warned... I've been all over the www.python.org site and googled, but I've not found just how to add new modules. I've tried setting PYTHONPATH, I've tried putting the new module directories into the site-packages directory, I've tried creating the .pth files, I've even done all three of these...
4
4311
by: Fabian Braennstroem | last post by:
Hi, I am pretty new to python and will use it mainly in combination with scientific packages. I am running ubuntu breezy right now and see that some packages are out of date. Do you have any suggestion, how I can get/keep the latest python modules (e.g. scipy, numpy,...) on my ubuntu system? I.e. does there exist any script/program, which...
17
5392
by: mohan | last post by:
Hi Guys, I've been using the following IDE, "Pythonwin - Python IDE and GUI Framework for Windows. Copyright 1994-2001 Mark Hammond " With respect to my work, I had created my own modules (.py files) in drives and folders other than the python root. I know that if I need to
3
1700
by: Tinka | last post by:
Hi, I'm having trouble with my new directory hierarchy. My little python programme worked as long as I had all my modules in one big file. I have decided now that this was just to messy and I couldn't find stuff right away anymore. So I wanted to put similar modules into packages. I have read the 'An Introduction to Python' by Guido van Rossum...
7
3242
by: tinnews | last post by:
I'm just beginning to create some python modules for my own use and I'm wondering where to put them. Initially I have put them in $HOME/bin and I have set PYTHONPATH to point to them there. It all seems to be OK but I was wondering if I might be storing up problems for the future by putting python modules in with my odds and sods of shell...
0
1513
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing depending modules that are in 'sibling' directories. Like in the following scenario: pkg_root\ -__init__ - common\ - - __init__
5
2460
by: John Ladasky | last post by:
Hi folks, Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux 7.04 desktop. I've gotten tired of maintaining multiple copies of my personal modules that I use over and over. I have copies of these files in the same directory as the main program I happen to be working on at the time. I've also downloaded FANN, and want...
16
7225
by: ssecorp | last post by:
Is there a way to view all the modules I have available for import from within Python? Like writing in the interpreter: import.modules Also, is there anything like Cpan for Python?
0
7832
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8262
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8129
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...
0
6498
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...
1
5643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3768
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...
1
2262
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
1
1364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1094
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...

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.