473,668 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

import directory error

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
3 1771
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********@inf osense.no>
infoSense AS / development
Jul 18 '05 #2
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
Problem solved thanks a lot
"Steve Holden" <st***@holdenwe b.com> a écrit dans le message de news:
f9sLd.101051$Jk 5.32898@lakerea d01...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6399
by: Markus Doering | last post by:
Hello, I just switched from 2.2 to Python 2.3. I am developing an XML/CGI interface to databases as a python package called "unitWrapper" containing several modules which ran fine under v2.2. Running Python2.3 under windows2000 I get import errors when a module imports another module of the same package with absolute package-names like this:
5
1818
by: Colin Brown | last post by:
I have instances where Python 2.3.2 import both does not work or error. It can be demonstrated under Win2k and Linux by doing the following: 1. Create a subdirectory "abc" 2. In directory "abc" create a blank "__init__.py" 3. In directory "abc" create a file "abc.py" containing "print 'this is abc'" 4. Interactively run python interpreter >>> import sys >>> sys.path.append('./abc')
4
1699
by: Matt Whiteley | last post by:
I have some python code in a directory called customModules and this sits under the main directory which contains a piece of server.py code. Under 2.1.x and 2.2.x, I did the following in server.py : import time, sys sys.path += from requestHandler import inputHandler and that allowed me to reference the requestHandler (stored in the
5
1631
by: Dfenestr8 | last post by:
Hi. I have a program which I want a plugin directory for. I figured the way to go about that would be to just add a plugin/ dir to sys.path, and import everything in it. Then my program can just execute the main() method of each imported plugin. Is that a good way to go about it? If so, how do I import everything in the plugins dir? The method raises an
0
2144
by: atlantis | last post by:
Hi, I have a very strange problem with xsl:import when usig RELATIVE path on AIX 5.2 server. I have two XSL files in the same directory: "ists_xslt3.xsl" and "ists_xslt3_layout.xsl". This is what the beginning of "ists_xslt3.xsl" looks like:
3
3524
by: David Jackson | last post by:
Hi all, I'm a real beginner with python but have what I think is a simple question. I am writing some simple modules and would like to place them into a subdirectory. But then I cannot seem to import them. I have tried the following. I wrote a module called fibo.py with some definitions in it (one called fibo). In the same directory, I wrote another file (test.py) and began with import fibo. This worked fine and I was able to use...
7
20552
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 _Ron
3
2433
by: gatoruss | last post by:
Newbie here with a (hopefully not) dumb question....tried searching around on the 'Net without success, so here goes.... I have been playing around with python. I wrote a script that sends an smtp email. Here is the link that I found explaining how such a script could be written Sending an Email As you see it imports a module as follows: "import smtplib". I am using a linux machine (ubuntu ditro). It has both python2.5 and python2.4...
0
1310
by: Victor Lin | last post by:
Hi, I got some problem with import. I have some python file that should be executed. Something like unit-test or other small tool. The project is not so small. So I want to separate these files into different packages. Might like this: project/
0
8459
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8374
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
8890
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...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
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
5677
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
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
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
1783
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.