Re: another problem with modules

2011-02-17 Thread MRAB

On 17/02/2011 17:11, Tim Hanson wrote:

Okay, I solved my problem with Python finding modules:

I put the following into a file in my home directory, on the good advice of
Andrea Crotti:

import sys
sys.path.append('/home/foo/mypath'

I named the file "~/pypath.py", so now, in idle:

import pypath

No errors.

I'm still getting a little frustrated loading modules.  I typed the following
little test function into idle:

def intersect(seq1,seq2):
   res=[]
   for x in seq1:
 if x in seq2:
   res.append(x)
   return res

intersect('spam','spmmer')


No big deal.  Runs fine.  Exited and re-entered idle,

import pypath
import intersect #the name of a file that contains the above short function.


This imports the module 'intersect'. The name 'intersect' refers to the
module itself.


intersect('spam','spmmer')


This tries to call the module. Perhaps you meant:

intersect.intersect('spam','spmmer')

If you have a module "foo", which contains a function "bar", then "foo"
refers to the module and "foo.bar" refers to the function.


Traceback (most recent call last):
   File "", line 1, in
 intersect('spam','spmmer')
TypeError: 'module' object is not callable




Huh?  Why doesn't this run when imported?  More importantly, how do I
interpret this error message so that I can find the problem myself next time?

--
http://mail.python.org/mailman/listinfo/python-list


Re: another problem with modules

2011-02-17 Thread Robert Kern

On 2/17/11 11:11 AM, Tim Hanson wrote:


import pypath
import intersect #the name of a file that contains the above short function.
intersect('spam','spmmer')

Traceback (most recent call last):
   File "", line 1, in
 intersect('spam','spmmer')
TypeError: 'module' object is not callable




Huh?  Why doesn't this run when imported?  More importantly, how do I
interpret this error message so that I can find the problem myself next time?


Please read the first section of this page about modules in the tutorial:

  http://docs.python.org/tutorial/modules.html#modules

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


another problem with modules

2011-02-17 Thread Tim Hanson
Okay, I solved my problem with Python finding modules:

I put the following into a file in my home directory, on the good advice of 
Andrea Crotti:

import sys
sys.path.append('/home/foo/mypath'

I named the file "~/pypath.py", so now, in idle:

import pypath

No errors.

I'm still getting a little frustrated loading modules.  I typed the following 
little test function into idle:

def intersect(seq1,seq2):
  res=[]
  for x in seq1:
if x in seq2:
  res.append(x)
  return res

intersect('spam','spmmer')


No big deal.  Runs fine.  Exited and re-entered idle, 

import pypath
import intersect #the name of a file that contains the above short function.
intersect('spam','spmmer')

Traceback (most recent call last):
  File "", line 1, in 
intersect('spam','spmmer')
TypeError: 'module' object is not callable
>>> 

Huh?  Why doesn't this run when imported?  More importantly, how do I 
interpret this error message so that I can find the problem myself next time?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: another problem..

2009-12-16 Thread Grant Edwards
On 2009-12-16, codefly  wrote:

> now.. another problem..

Sorry to be so blunt, but your main problem is that you don't
know how to ask questions.  Fix that, and everything else will
become much easier:

 1) Read this:  http://catb.org/~esr/faqs/smart-questions.html

 2) Read it again.

 3) Read these sections one more time:

 http://catb.org/~esr/faqs/smart-questions.html#beprecise
 http://catb.org/~esr/faqs/smart-questions.html#code
  
-- 
Grant Edwards   grante Yow! Is it NOUVELLE
  at   CUISINE when 3 olives are
   visi.comstruggling with a scallop
   in a plate of SAUCE MORNAY?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: another problem..

2009-12-16 Thread Dave Angel



codefly wrote:

now.. another problem..
when i type me = code2()
the error is here..
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'module' object is not callable

  
By creating a new thread for each new question, you're forcing yourself 
to repeat the code and environment answers, which you did not.  So let's 
ignore this thread, and I'll respond on the previous one.


--
http://mail.python.org/mailman/listinfo/python-list


another problem..

2009-12-16 Thread codefly
now.. another problem..
when i type me = code2()
the error is here..
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'module' object is not callable
-- 
http://mail.python.org/mailman/listinfo/python-list


Another problem with 'Dive Into Python'

2006-07-31 Thread Ben Edwards (lists)
Am going through Chapter 9 - HTTP Web Services in dive into Python.  It
uses the following:

data = urllib.urlopen('http://diveintomark.org/xml/atom.xml').read()

The page no longer exists, can anyone recommend an alternative page to
use?

Ben



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-17 Thread Amir Dekel
Jeff Shannon wrote:
Judging from this, I think that os.environ['USERPROFILE'] seems like it 
may do what you want, though os.environ['APPDATA'] might be useful as 
well.  Of course, if you're trying to get something to work 
cross-platform, things may be more difficult -- but that's because 
Windows doesn't normally use ~ so its use is not supported very well.  
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Both problems solved! The .pth seems like the best solution for the 
first one, and for the expanduser("~") I just added a $HOME variable to 
the Environment variables of WinXP. Simple solutions. Thanks Jeff

Amir
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Brian van den Broek
Jeff Shannon said unto the world upon 2004-12-16 17:54:
Amir Dekel wrote:
2. os.path.expanduser("~") always gives me "C:\\" instead of my 
homepath. I have tried to change my homepath in WinXP using set 
homepath(in command line), and alsaw using the "Environment Variables" 
in WinXP system properties, non helped. I really have to fix this 
somehow.

Well, according to os.path.expanduser()'s docstring, it uses the $HOME 
environment variable to determine how to expand ~.  I don't know what's 
standard on Windows, but I tried checking for a $HOME and found none.  
Here's (a slightly redacted copy of) what I *do* find (Win2K):

Judging from this, I think that os.environ['USERPROFILE'] seems like it 
may do what you want, though os.environ['APPDATA'] might be useful as 
well.  Of course, if you're trying to get something to work 
cross-platform, things may be more difficult -- but that's because 
Windows doesn't normally use ~ so its use is not supported very well.  
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Jeff Shannon
Technician/Programmer
Credit International
Hi all,
some 'nix-world tool (probably xemacs) that I put on my WinMe box didn't 
like the lack of a HOME environment variable, either. This was easily 
solved by adding the line

SET HOME=D:\HOME
to my autoexec.bat file. (I don't know if this works in more recent 
versions of Windows, but I'd imagine so.) There to, I have added a line

SET PYTHONPATH=D:\List_Of_Dirs;D:\Use_a_semi_colon_to_sep_items_on_Win
Which puts those dirs into sys.path (at the front, I believe). I also 
used the .pth file trick because they two means differ as to where in 
the sys.path they add their entries.

Best to all,
Brian vdB
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Fernando Perez
Amir Dekel wrote:

> Hi everyone,
> 
> I have two problems:
> 
> 1. How can I keep my changes in sys.path after closing the interpreter?

As others said, use $PYTHONPATH

> 2. os.path.expanduser("~") always gives me "C:\\" instead of my
> homepath. I have tried to change my homepath in WinXP using set
> homepath(in command line), and alsaw using the "Environment Variables"
> in WinXP system properties, non helped. I really have to fix this somehow.

This is what ipython uses to try and get that information in a portable manner. 
Note that it uses _winreg, which is part of the win32 extensions.

In [1]: import IPython.genutils

In [2]: psource IPython.genutils.get_home_dir
def get_home_dir():
"""Return the closest possible equivalent to a 'home' directory.

For Posix systems, this is $HOME, and on NT it's $HOMEDRIVE\$HOMEPATH.

Currently only Posix and NT are implemented, a HomeDirError exception is
raised for all other OSes. """ #'

if os.name == 'posix':
return os.environ['HOME']
elif os.name == 'nt':
# For some strange reason, win9x returns 'nt' for os.name.
try:
return os.path.join(os.environ['HOMEDRIVE'],os.environ['HOMEPATH'])
except:
try:
# Use the registry to get the 'My Documents' folder.
import _winreg as wreg
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
   
"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders")
homedir = wreg.QueryValueEx(key,'Personal')[0]
key.Close()
return homedir
except:
return 'C:\\'
elif os.name == 'dos':
# Desperate, may do absurd things in classic MacOS. May work under DOS.
return 'C:\\'
else:
raise HomeDirError,'support for your operating system not implemented.'

HTH,

f

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Amir Dekel
Jeff Shannon wrote:
Amir Dekel wrote:
2. os.path.expanduser("~") always gives me "C:\\" instead of my 
homepath. I have tried to change my homepath in WinXP using set 
homepath(in command line), and alsaw using the "Environment Variables" 
in WinXP system properties, non helped. I really have to fix this 
somehow.

Judging from this, I think that os.environ['USERPROFILE'] seems like it 
may do what you want, though os.environ['APPDATA'] might be useful as 
well.  Of course, if you're trying to get something to work 
cross-platform, things may be more difficult -- but that's because 
Windows doesn't normally use ~ so its use is not supported very well.  
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Yet another reason not to use Windows and all it's silly system... 
Unfotunely, I am stuck with a USB ADSL modem on a family computer, which 
led to terrible headaches with my (quiet successful, actually) attempts 
to install Linux.

Well, so be it...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Jeff Shannon
Amir Dekel wrote:
2. os.path.expanduser("~") always gives me "C:\\" instead of my 
homepath. I have tried to change my homepath in WinXP using set 
homepath(in command line), and alsaw using the "Environment Variables" 
in WinXP system properties, non helped. I really have to fix this 
somehow.

Well, according to os.path.expanduser()'s docstring, it uses the $HOME 
environment variable to determine how to expand ~.  I don't know what's 
standard on Windows, but I tried checking for a $HOME and found none.  
Here's (a slightly redacted copy of) what I *do* find (Win2K):

>>> for key, val in os.environ.items():
... print '%15s   %s' % (key, val)
...
   TMP   C:\DOCUME~1\Jeff\LOCALS~1\Temp
  USERNAME   jeff
  COMPUTERNAME   ##
   LOGONSERVER   ##
   COMSPEC   C:\WINNT\system32\cmd.exe
USERDOMAIN   ###
COMMONPROGRAMFILES   C:\Program Files\Common Files
PROCESSOR_IDENTIFIER   x86 Family #...
  PROGRAMFILES   C:\Program Files
PROCESSOR_REVISION   0806
   PATHEXT   .COM;.EXE;.BAT;.CMD;#
SYSTEMROOT   C:\WINNT
  PATH   C:\Python22\.;C:\WINNT\system32;C:\WINNT;##
   APPDATA   C:\Documents and Settings\Jeff\Application Data
  TEMP   C:\DOCUME~1\Jeff\LOCALS~1\Temp
 HOMEDRIVE   C:
   SYSTEMDRIVE   C:
PROCESSOR_ARCHITECTURE   x86
NUMBER_OF_PROCESSORS   1
ALLUSERSPROFILE   C:\Documents and Settings\All Users.WINNT
PROCESSOR_LEVEL   6
  HOMEPATH   \
OS2LIBPATH   C:\WINNT\system32\os2\dll;
   USERPROFILE   C:\Documents and Settings\Jeff
OS   Windows_NT
WINDIR   C:\WINNT
>>>

Judging from this, I think that os.environ['USERPROFILE'] seems like it 
may do what you want, though os.environ['APPDATA'] might be useful as 
well.  Of course, if you're trying to get something to work 
cross-platform, things may be more difficult -- but that's because 
Windows doesn't normally use ~ so its use is not supported very well.  
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Jeff Shannon
Technician/Programmer
Credit International
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Amir Dekel
Jeff Shannon wrote:
Not only can one modify the environment variable PYTHONPATH, but one can 
also use a .pth file.  Under windows, when the interpreter starts up it 
will search its default sys.path for any files with extension .pth; if 
it finds any, then it will use each line of those files as a directory 
to add to sys.path.  Thus, if you drop a mymodule.pth file in 
site-packages, which contains a list of the directories you're 
interested in, sys.path will automatically be amended for you every time 
Python starts.

Works great! So simple, yet people get so confused...
Do you have a solution for my other problem?
Thanks,
Amir
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Jeff Shannon
Lenard Lindstrom wrote:
Amir Dekel <[EMAIL PROTECTED]> writes:
 

Hi everyone,
I have two problems:
1. How can I keep my changes in sys.path after closing the interpreter?
   

Saving sys.path changes between interpreter runs would be more involved.
On Windows Python loads the initial module search path from the registry.
Python's registry entries are made during installation and are left alone
afterwards. Changing these entries is probably not a good idea. But
if you need sys.path persistence I can try writing an example that does
it automatically using the atexit module and a history file.
 

Not only can one modify the environment variable PYTHONPATH, but one can 
also use a .pth file.  Under windows, when the interpreter starts up it 
will search its default sys.path for any files with extension .pth; if 
it finds any, then it will use each line of those files as a directory 
to add to sys.path.  Thus, if you drop a mymodule.pth file in 
site-packages, which contains a list of the directories you're 
interested in, sys.path will automatically be amended for you every time 
Python starts.

Jeff Shannon
Technician/Programmer
Credit International
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Jean Brouwers

Maybe, set environment variable PYTHONPATH.  More details at



/Jean Brouwers



In article <[EMAIL PROTECTED]>, Lenard Lindstrom <[EMAIL PROTECTED]>
wrote:

> Amir Dekel <[EMAIL PROTECTED]> writes:
> 
> > Hi everyone,
> > 
> > I have two problems:
> > 
> > 1. How can I keep my changes in sys.path after closing the interpreter?
> > 
> Just how flexible does this need to be? sys.path can easily be altered
> at startup from within a sitecustomize module (section 3.28 -- site --
> Python Runtime Services -- Python Library Reference) or a module specified
> by the PYTHONSTARTUP environment variable (section 3.29 -- user -- Python
> Runtime Services). Just have one of these modules append to sys.path any
> additional paths you need.
> 
> Saving sys.path changes between interpreter runs would be more involved.
> On Windows Python loads the initial module search path from the registry.
> Python's registry entries are made during installation and are left alone
> afterwards. Changing these entries is probably not a good idea. But
> if you need sys.path persistence I can try writing an example that does
> it automatically using the atexit module and a history file.
> 
> Lenard Lindstrom
> <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Lenard Lindstrom
Amir Dekel <[EMAIL PROTECTED]> writes:

> Hi everyone,
> 
> I have two problems:
> 
> 1. How can I keep my changes in sys.path after closing the interpreter?
> 
Just how flexible does this need to be? sys.path can easily be altered
at startup from within a sitecustomize module (section 3.28 -- site --
Python Runtime Services -- Python Library Reference) or a module specified
by the PYTHONSTARTUP environment variable (section 3.29 -- user -- Python
Runtime Services). Just have one of these modules append to sys.path any
additional paths you need.

Saving sys.path changes between interpreter runs would be more involved.
On Windows Python loads the initial module search path from the registry.
Python's registry entries are made during installation and are left alone
afterwards. Changing these entries is probably not a good idea. But
if you need sys.path persistence I can try writing an example that does
it automatically using the atexit module and a history file.

Lenard Lindstrom
<[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Adding paths to sys.path permanently, and another problem...

2004-12-16 Thread Amir Dekel
Hi everyone,
I have two problems:
1. How can I keep my changes in sys.path after closing the interpreter?
2. os.path.expanduser("~") always gives me "C:\\" instead of my 
homepath. I have tried to change my homepath in WinXP using set 
homepath(in command line), and alsaw using the "Environment Variables" 
in WinXP system properties, non helped. I really have to fix this somehow.

Thanks,
Amir
--
http://mail.python.org/mailman/listinfo/python-list