472,958 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

importing a package

Hi,

I developed a package with a structure like this
src/
tesfile.py
dir1/
__init__.py
file1.py
dir2/
__init__.py
file2.py

The testfile is meant to try the code out that is specified in file1.py
and file2.py

Now i have another project where i want to use the code from that package.
The structure of that project:

src/
file3.py
dir3/
__init__.py
file4.py

To use it, i copied the package in the root of the project:
src/
file3.py
dir3/
__init__.py
file4.py
package/
__init__.py
dir1/
__init__.py
file1.py
dir2/
__init__.py
file2.py

In my code (file3.py) i then do an "import package.dir1.file1 as myobj"
and access a class like this:
myobj.LMyClass()

(where myobj and LMyClass are decent names. Used these as example)

That works but i get an error in the package files.
I then get an error in package/dir1/file1.py on an import statement
specified in that file1.py that says import dir2.file2

How come this works as standalone project and not when i try to use it
as in situation 2 it doesn't seem to find module file2.py ?
How can i solve that?

Thanks
Benedict
Jul 19 '05 #1
4 1530
> I developed a package with a structure like this
src/
tesfile.py
dir1/
__init__.py
file1.py
dir2/
__init__.py
file2.py


Importing dir2/file2 from dir1/file1.py works here, because when yuo started
the testfile script the src/ directory was added to the sys.path list.

If you relocate dir1/ and dir2/ in a "package" directory here it will not
work.
--
damjan
Jul 19 '05 #2
Damjan wrote:
I developed a package with a structure like this
src/
tesfile.py
dir1/
__init__.py
file1.py
dir2/
__init__.py
file2.py

Importing dir2/file2 from dir1/file1.py works here, because when yuo started
the testfile script the src/ directory was added to the sys.path list.

If you relocate dir1/ and dir2/ in a "package" directory here it will not
work.


Indeed, when i do this, then it works
import sys
sys.path.append('package')

However, why is it that package isn't added automatically to the pad?

Regards,
Benedict
Jul 19 '05 #3
> Indeed, when i do this, then it works
import sys
sys.path.append('package')

However, why is it that package isn't added automatically to the pad?


When you execute a python program the directory where the program is is
automatically added to sys.path. No other directory is added to the default
builtin sys.path.

In you case (the second case), you can import package.dir2.file2.
--
damjan
Jul 19 '05 #4
Damjan wrote:
Indeed, when i do this, then it works
import sys
sys.path.append('package')

However, why is it that package isn't added automatically to the pad?

When you execute a python program the directory where the program is is
automatically added to sys.path. No other directory is added to the default
builtin sys.path.

In you case (the second case), you can import package.dir2.file2.


OK, thanks for the info

Regards,
Benedict

--
Benedict Verheyen Debian User
http://www.heimdallit.be Public Key 0x712CBB8D
Jul 19 '05 #5

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

Similar topics

0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
1
by: yzhao12 | last post by:
Hi everyone, I encountered an error "Need to run the object to perform this operation Code execution exception: EXCEPTION_ACCESS_VIOLATION " When I try to import data from Oracle to MS...
4
by: Little PussyCat | last post by:
Hello, I nee to write something that will transfer excel data into an SQL Server table. I have for another database application I wrote have it importing Excel spreadsheet data using cell by...
3
by: Charles Krug | last post by:
List: I'm trying to use the example files from Programming Python, 2nd Ed. I've copied them into c:\Python24\Examples\PP2E. Launching any of the examples programs by themselves seems to work...
3
by: Elmo Watson | last post by:
I've been asked to develop a semi-automated type situation where we have a database table (sql server) and periodically, there will be a comma delimited file from which we need to import the data,...
2
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to...
0
by: fboweb.com | last post by:
Hey there! I have a DTS import package from an old SQL server that takes flat text files, does some munging on the columns using ActiveX scripts and imports those flat files to various tables. ...
3
by: MyPetSlug | last post by:
Hello Guys, Using Pythong 2.4 and py2exe 0.6.6. So, I've created a wxPython application that I use for testing. The idea is to have a tests directory so that every time the application starts, it...
1
by: mypetslug | last post by:
Hi, I'm sorry if this has been asked before, but I can't seem to find an answer to this anywhere and so far, trial and error hasn't gotten me far either. Using python 2.4, I've created a...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.