472,811 Members | 1,613 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,811 software developers and data experts.

import confused by contents of working directory

Jon
It appears that (windows) python searches in the current working
directory before looking in the local site-packages directory, or that
'.' comes first in sys.path? The problem arises when I made the mistake
of running a test program from the same directory where I built and
installed my package. Python uses the package from the current
directory, which misses the pyd files, instead of the site-packages
package which has them installed.

Would somebody please explain how to know what is going on when an
import statement is ambiguous? If the interpreter has several choices
about what to import then which one is chosen? I apologise if this is
FAQ, but it seems that if I say "import mymodule" and the interpreter
finds more than one option for mymodule it could raise an Exception
asking "which one do you want??" instead of silently picking up the
first thing it finds?

This would be perhaps a major change - but I expect "import" to go look
in the python installation and would look for a command like "load" to
go looking for specific filenames or find things that are hanging
around in the current working directory. Is there any plan for a
statement like:

"from future import PleaseDontJustGuessWhatToImport" ?

Thanks in advance for any useful advice, perhaps I am just missing the
out on the "right way to do it"? It is normal to remove '.' from
sys.path at the start of a script?

Jon

-------------------------------------
Some examples of my confusion are outlined below. I use Numeric only
because I assume they are doing a proper installation. I had thought
for a long time that I had this problem because I was screwing up with
distutils or .pth files etc etc.

c:\> mkdir import

c:\> cd import

c:\import> python -c "import Numeric; print 'OK!'"
OK!

c:\import> echo raise Exception("Not OK! Local py file") > Numeric.py

c:\import> python -c "import Numeric;print 'OK!'"
Traceback (most recent call last):
File "<string>", line 1, in ?
File "Numeric.py", line 1, in ?
raise Exception("Not OK! Local py file")
Exception: Not OK! Local py file

c:\import> del Numeric.py

c:\import> mkdir Numeric

c:\import> python -c "import Numeric; print 'OK!'"
OK!

c:\import> echo raise Exception("Not OK! Local version") >
Numeric\__init__.py

c:\import> python -c "import Numeric;print 'OK!'"
Traceback (most recent call last):
File "<string>", line 1, in ?
File "Numeric\__init__.py", line 1, in ?
raise Exception("Not OK! Local version")
Exception: Not OK! Local version

c:\import> del Numeric\__init__.py

c:\import>python -c "import Numeric;print 'OK!'"
Traceback (most recent call last):
File "<string>", line 1, in ?
File "c:\python24\lib\site-packages\PIL\__init__.py", line 1, in ?
#
Exception: Not OK! Local version

[authors note] What??? This was imported from Numeric\__init__.pyc

c:\import> del Numeric\__init__.pyc

c:\import> python -c "import Numeric;print 'OK!'"
OK!

The the actual example is more like this:

c:\ImageD11> python setup.py build --compiler=mingw32 install
....
c:\ImageD11>c:\python24\scripts\ImageD11_gui.py
....
ImportError: can't import name closest

c:\ImageD11> cd ..

c:\> c:\python24\scripts\ImageD11_gui.py
....and the gui runs!! ...

Given that the script resides in $python\scripts I sort of expect it to
avoid tripping over some local and irrelevant file.

Jun 2 '06 #1
2 2505
"Jon" wrote:
Would somebody please explain how to know what is going on when an
import statement is ambiguous?
there is no ambiguity: python searches through a list of directories, in a given
order.

the list is stored in the sys.path list, which you can inspect and modify freely.

also see the section "The Module Search Path" in the Modules chapter in the
tutorial:

http://pytut.infogami.com/node8.html
If the interpreter has several choices about what to import then which one is
chosen?


the first one on the path.

</F>

Jun 2 '06 #2
Jon wrote:
It appears that (windows) python searches in the current working
directory before looking in the local site-packages directory, or that
'.' comes first in sys.path?


Unless it behaves very differently on Windows from
everything else, this isn't quite true. The current
directory is only put in the path when using the
interpreter interactively. When you do

python somefile.py

the directory containing somefile.py is put at the
beginning of sys.path, not the current directory.
So you'd still have the same problem even if you
weren't cd'ed to the directory containing the test
program.

The moral is not to put your test program in the
same directory as your package files. (Or if you
must, have it delete sys.path[0] before importing
anything.)

--
Greg
Jun 3 '06 #3

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

Similar topics

3
by: Olivier Noblanc ATOUSOFT | last post by:
Hello, When i want to import a .py fire from another subdirectory i make import inc/setupxml but that make me an error message.
6
by: John J. Lee | last post by:
I'm tearing my hair out at what seems like weird import behaviour I'm getting from Python's stdlib test script, regrtest.py (not for the first time: seem to have forgotten the resolution from last...
2
by: danielmcbrearty | last post by:
Hi Old hand at perl, giving python a try. Trying to get pymedia running on my winxp box. Have installed activestate python 2.4 and pymedia using precompiled installers. Problem is it SEEMS...
4
by: gsocks | last post by:
hello, i have a problem. i would like to import python files above and below my current directory. i'm working on /home/foo/bar/jar.py i would like to import /home/foo/car.py and...
3
by: Frank Millman | last post by:
Hi all I am writing a business/accounting application. Once a user has logged in they are presented with a menu. Each menu option has a description and an associated file name and program name....
1
by: Steve | last post by:
Hi All, I have an Excel file being delivered to a shared drive where I only have read access on a daily basis. The files arrive in the following format 'Filename+timestamp.xls' Example:...
7
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 ...
2
by: Bill Jackson | last post by:
Once again, I am having issues with imports... Until now, I thought the general guidelines were to rarely use 'from x import y' syntax, except when you really want to copy names over. However, I...
0
by: Laszlo Nagy | last post by:
ohad frand wrote: So this is what you have: /1/tmp1.py /1/tmp2.py /2/tmp1.py /2/tmp2.py
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 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...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
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...

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.