By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

import directory error

P: n/a
Hello,
When i want to import a .py fire from another subdirectory i make

import inc/setupxml
but that make me an error message.

A man tell me to put a dot but that doesn't work.

Can you help me ?

Thanks.

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Olivier Noblanc ATOUSOFT wrote:
Hello,
When i want to import a .py fire from another subdirectory i make

import inc/setupxml
but that make me an error message.

A man tell me to put a dot but that doesn't work.

Can you help me ?

Thanks.


You should write

import inc.setupxml

this imports the module located at inc/setupxml.py
It's said that it imports the setupxml *module* from the inc *package*
All packages should include a __init__.py file. The import may not work
unless. You will get the __init__.py file if you type

import inc

--
--------------------------------------
Ola Natvig <ol********@infosense.no>
infoSense AS / development
Jul 18 '05 #2

P: n/a
Olivier Noblanc ATOUSOFT wrote:
Hello,
When i want to import a .py fire from another subdirectory i make

import inc/setupxml
but that make me an error message.

A man tell me to put a dot but that doesn't work.

Can you help me ?

Thanks.

If you want to import a single .py (a Python module) then the ONLY way
to achieve that is to make sure it appears in a directory that is a
member of the sys.path list. (This is a slight simplification, but it
will do as long as you are only importing from the file store).

There are various ways to affect the contents of sys.path, the best
known of which include

1. Setting the PYTHONPATH environment variable
2. Creating *.pth files
3. Altering sys.path inside site-customize.py in
your standard library

Python does allow you to implement PACKAGES, which are directories
containing

a) a file called __init__.py and (optionally)
b) other modules (.py files) and packages (directories
containing __init__.py files).

The Python interpreter looks for packages in all the same places it
looks for modules, but it imports packages by running the __init__.py
file (as usual, this happens on the *first* time the package is imported).

So, for example, under Cygwin or Linux/Unix, I can define a package
(with no Python in it, but still obeying the rules) as follows:

sholden@dellboy ~
$ mkdir mp1

sholden@dellboy ~
$ touch mp1/__init__.py

sholden@dellboy ~
$ touch mp1/rhubarb.py

sholden@dellboy ~
$ mkdir mp1/p2

sholden@dellboy ~
$ touch mp1/p2/__init__.py

sholden@dellboy ~
$ python
Python 2.4 (#1, Dec 4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
import sys
"" in sys.path True import mp1
import mp1.rhubarb
import mp1.p2


sholden@dellboy ~
$ find mp1
mp1
mp1/p2
mp1/p2/__init__.py
mp1/p2/__init__.pyc
mp1/rhubarb.py
mp1/rhubarb.pyc
mp1/__init__.py
mp1/__init__.pyc

In this case mp1.rhubarb is a module from the mp1 package, mp1.p2 is a
sub-package of mp1. You can see what's been compiled by the interpreter
on import and when by looking at the .pyc files.

Does this help any?

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #3

P: n/a
Problem solved thanks a lot
"Steve Holden" <st***@holdenweb.com> a écrit dans le message de news:
f9sLd.101051$Jk5.32898@lakeread01...
Olivier Noblanc ATOUSOFT wrote:
Hello,
When i want to import a .py fire from another subdirectory i make

import inc/setupxml
but that make me an error message.

A man tell me to put a dot but that doesn't work.

Can you help me ?

Thanks.

If you want to import a single .py (a Python module) then the ONLY way to
achieve that is to make sure it appears in a directory that is a member of
the sys.path list. (This is a slight simplification, but it will do as
long as you are only importing from the file store).

There are various ways to affect the contents of sys.path, the best known
of which include

1. Setting the PYTHONPATH environment variable
2. Creating *.pth files
3. Altering sys.path inside site-customize.py in
your standard library

Python does allow you to implement PACKAGES, which are directories
containing

a) a file called __init__.py and (optionally)
b) other modules (.py files) and packages (directories
containing __init__.py files).

The Python interpreter looks for packages in all the same places it looks
for modules, but it imports packages by running the __init__.py file (as
usual, this happens on the *first* time the package is imported).

So, for example, under Cygwin or Linux/Unix, I can define a package (with
no Python in it, but still obeying the rules) as follows:

sholden@dellboy ~
$ mkdir mp1

sholden@dellboy ~
$ touch mp1/__init__.py

sholden@dellboy ~
$ touch mp1/rhubarb.py

sholden@dellboy ~
$ mkdir mp1/p2

sholden@dellboy ~
$ touch mp1/p2/__init__.py

sholden@dellboy ~
$ python
Python 2.4 (#1, Dec 4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> "" in sys.path True >>> import mp1
>>> import mp1.rhubarb
>>> import mp1.p2
>>>


sholden@dellboy ~
$ find mp1
mp1
mp1/p2
mp1/p2/__init__.py
mp1/p2/__init__.pyc
mp1/rhubarb.py
mp1/rhubarb.pyc
mp1/__init__.py
mp1/__init__.pyc

In this case mp1.rhubarb is a module from the mp1 package, mp1.p2 is a
sub-package of mp1. You can see what's been compiled by the interpreter on
import and when by looking at the .pyc files.

Does this help any?

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.