Re: How can I import a py script by its absolute path name?

2005-07-17 Thread J.Bijsterbosch
Hello James,

James Dennett [EMAIL PROTECTED] schreef in bericht
news:[EMAIL PROTECTED]
 J.Bijsterbosch wrote:

[ snip ]
 and didn't remember Windows uses path names which need special
 treatment.
 
  Hmm, what you call special treatmentg comes from pythons deep
underlying C
  and C++ language heietidge I presume. A backslash in a C or C++ string
means
  the following character is a so called escape character, like \n
represents
  a newline and \r a return to the beginning of a line.
  If you really want a backslash you need to type it twice like so \\. Has
  nothing to do with Windows...;-))

 Actually, it does have a connection to Windows.

 On Unix, backslashes are rarely used for anything *except* escape
 characters.  Pathnames tend not to include backslashes, so in most
 cases it's not necessary to escape backslashes in path names.

I knowg, I've had mandrake installed for some time until that pc died on
me, the pc that is, not mandrake...

 On Windows, however, backslash is a valid path separator, and must be
 escaped.

 So, on Unix, for a path separator, you type /.  On Windows you
 can either do the same, or type \\.  (Or (ab)use raw strings.)

Okay, point taken, but I still think it's more a C(++) string thing than a
Windows
issue. I could of course argue that the backslash path separator is there
for backward
compatebility with Dos, but I won't, much to off topic...;-))

  James

Greetings from overcast Amsterdam,

Jan


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


Re: How can I import a py script by its absolute path name?

2005-07-16 Thread Edvard Majakari
J.Bijsterbosch [EMAIL PROTECTED] writes:

 Hmm, what you call special treatmentg comes from pythons deep underlying C
 and C++ language heietidge I presume. A backslash in a C or C++ string means
 the following character is a so called escape character, like \n represents
 a newline and \r a return to the beginning of a line.
 If you really want a backslash you need to type it twice like so \\. Has
 nothing to do with Windows...;-))

Yes, I'm well aware of that. However, you can say that using '\' as a path
separator needs special treatment, because it is conventionally treated as an
escape character. Moreover, I wans't the one asking for information, I have
privilidge to use real operating systems as a programming platform. Thanks for
enthsiasm, though :)

-- 
# Edvard Majakari   Software Engineer
# PGP PUBLIC KEY available  Soli Deo Gloria!
You shouldn't verb verbs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I import a py script by its absolute path name?

2005-07-15 Thread Chris Lambacher
You probably actually want:

import sys
sys.path.instert(0, r'c:\xxx\yyy')
m = __import__('zzz', globals(),  locals(), [])
del sys.path[0]

Because if another module named zzz exists in your path.  Appending will pick
those versions up first.  Then you delete the path you just added so that you
don't have any problems importing other modules that may have the same names a
python files in the path you just added.

-Chris
On Thu, Jul 14, 2005 at 02:52:31PM +0300, Edvard Majakari wrote:
 could ildg [EMAIL PROTECTED] writes:
 
  I want to import c:\xxx\yyy\zzz.py into my programme,
  What should I do?
  Thank you~
 
 import sys
 sys.path.append('c:\xxx\yyy')
 import zzz
 
 (Untested, similar idiom would work in *nix systems, never programmed in
 Windows)
 
 However, I guess it is not very usual you should need to import stuff from
 arbitrary locations. Consider publishing those modules in normal Python
 include path (just see what ''print sys.path'' produces)
 
 -- 
 # Edvard Majakari Software Engineer
 # PGP PUBLIC KEY availableSoli Deo Gloria!
 
 $_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
 join('',map{chr hex}(split/(\w{2})/)),uc 
 substr(crypt(60281449,'es'),2,4),\n;
 -- 
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I import a py script by its absolute path name?

2005-07-15 Thread could ildg
Thank you for your help.
It's really useful for me.

On 7/14/05, Chris Lambacher [EMAIL PROTECTED] wrote:
 You probably actually want:
 
 import sys
 sys.path.instert(0, r'c:\xxx\yyy')
 m = __import__('zzz', globals(),  locals(), [])
 del sys.path[0]
 
 Because if another module named zzz exists in your path.  Appending will pick
 those versions up first.  Then you delete the path you just added so that you
 don't have any problems importing other modules that may have the same names a
 python files in the path you just added.
 
 -Chris
 On Thu, Jul 14, 2005 at 02:52:31PM +0300, Edvard Majakari wrote:
  could ildg [EMAIL PROTECTED] writes:
 
   I want to import c:\xxx\yyy\zzz.py into my programme,
   What should I do?
   Thank you~
 
  import sys
  sys.path.append('c:\xxx\yyy')
  import zzz
 
  (Untested, similar idiom would work in *nix systems, never programmed in
  Windows)
 
  However, I guess it is not very usual you should need to import stuff from
  arbitrary locations. Consider publishing those modules in normal Python
  include path (just see what ''print sys.path'' produces)
 
  --
  # Edvard Majakari Software Engineer
  # PGP PUBLIC KEY availableSoli Deo Gloria!
 
  $_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
  join('',map{chr hex}(split/(\w{2})/)),uc 
  substr(crypt(60281449,'es'),2,4),\n;
  --
  http://mail.python.org/mailman/listinfo/python-list
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: How can I import a py script by its absolute path name?

2005-07-15 Thread J.Bijsterbosch
Hello Edward,

Edvard Majakari [EMAIL PROTECTED] schreef in bericht
news:[EMAIL PROTECTED]
 Thorsten Kampe [EMAIL PROTECTED] writes:

  sys.path.append('c:\\xxx\\yyy') or sys.path.append('c:/xxx/yyy')

 Well, of course. As I said, it was untested :) I just copied the path
string,
 and didn't remember Windows uses path names which need special
 treatment.

Hmm, what you call special treatmentg comes from pythons deep underlying C
and C++ language heietidge I presume. A backslash in a C or C++ string means
the following character is a so called escape character, like \n represents
a newline and \r a return to the beginning of a line.
If you really want a backslash you need to type it twice like so \\. Has
nothing to do with Windows...;-))

Greetings from sunny Amsterdam,

Jan


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


Re: How can I import a py script by its absolute path name?

2005-07-15 Thread Terry Hancock
On Thursday 14 July 2005 07:43 am, Thorsten Kampe wrote:
 * Edvard Majakari (2005-07-14 12:52 +0100)
  could ildg [EMAIL PROTECTED] writes:
  I want to import c:\xxx\yyy\zzz.py into my programme,
  What should I do?
  Thank you~
  
  import sys
  sys.path.append('c:\xxx\yyy')
 
 sys.path.append('c:\\xxx\\yyy') or sys.path.append('c:/xxx/yyy')

While this will work, I think the OP may want something simpler:

my_mod = __import__('c:\\xxx\\yyy\\mymodule.py')

especially if this is following up on the relative path workaround (in
which case the result will be buried in the relative path import function).

The sys.path solution is the technique you should be using to
establish a top-level directory for your package.  Once you do
that, you can use __init__.py and dotted imports to get to everything
in your package by absolute paths (that is, relative to the top-level
package, rather than to each sub-package).  This is the preferred
Python approach in the current design.

However, the idea that it would be desireable to import packages by
something like ../main_package/other_subpackage/module2.py
has been suggested, and you can implement something like this using
the __import__ built-in as suggested above.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com


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


Re: How can I import a py script by its absolute path name?

2005-07-15 Thread James Dennett
J.Bijsterbosch wrote:

 Hello Edward,
 
 Edvard Majakari [EMAIL PROTECTED] schreef in bericht
 news:[EMAIL PROTECTED]
 
Thorsten Kampe [EMAIL PROTECTED] writes:


sys.path.append('c:\\xxx\\yyy') or sys.path.append('c:/xxx/yyy')

Well, of course. As I said, it was untested :) I just copied the path
 
 string,
 
and didn't remember Windows uses path names which need special
treatment.
 
 
 Hmm, what you call special treatmentg comes from pythons deep underlying C
 and C++ language heietidge I presume. A backslash in a C or C++ string means
 the following character is a so called escape character, like \n represents
 a newline and \r a return to the beginning of a line.
 If you really want a backslash you need to type it twice like so \\. Has
 nothing to do with Windows...;-))

Actually, it does have a connection to Windows.

On Unix, backslashes are rarely used for anything *except* escape
characters.  Pathnames tend not to include backslashes, so in most
cases it's not necessary to escape backslashes in path names.  On
Windows, however, backslash is a valid path separator, and must be
escaped.

So, on Unix, for a path separator, you type /.  On Windows you
can either do the same, or type \\.  (Or (ab)use raw strings.)

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


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread Thorsten Kampe
* Edvard Majakari (2005-07-14 12:52 +0100)
 could ildg [EMAIL PROTECTED] writes:
 I want to import c:\xxx\yyy\zzz.py into my programme,
 What should I do?
 Thank you~
 
 import sys
 sys.path.append('c:\xxx\yyy')

sys.path.append('c:\\xxx\\yyy') or sys.path.append('c:/xxx/yyy')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread Edvard Majakari
Thorsten Kampe [EMAIL PROTECTED] writes:


 sys.path.append('c:\\xxx\\yyy') or sys.path.append('c:/xxx/yyy')

Well, of course. As I said, it was untested :) I just copied the path string,
and didn't remember Windows uses path names which need special
treatment. One more reason to avoid inferior platforms :-

-- 
#!/usr/bin/perl -w
$h={23,69,28,'6e',2,64,3,76,7,20,13,61,8,'4d',24,73,10,'6a',12,'6b',21,68,14,
72,16,'2c',17,20,9,61,11,61,25,74,4,61,1,45,29,20,5,72,18,61,15,69,20,43,26,
69,19,20,6,64,27,61,22,72};$_=join'',map{chr hex $h-{$_}}sort{$a=$b}
keys%$h;m/(\w).*\s(\w+)/x;$_.=uc substr(crypt(join('',60,28,14,49),join'',
map{lc}($1,substr $2,4,1)),2,4).\n; print;
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread John Machin
Thorsten Kampe wrote:
 * Edvard Majakari (2005-07-14 12:52 +0100)
 
could ildg [EMAIL PROTECTED] writes:

I want to import c:\xxx\yyy\zzz.py into my programme,
What should I do?
Thank you~

import sys
sys.path.append('c:\xxx\yyy')
 
 
 sys.path.append('c:\\xxx\\yyy') or sys.path.append('c:/xxx/yyy')

or  sys.path.append(r'c:\xxx\yyy')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread Ralf W. Grosse-Kunstleve
% python
Python 2.4.1 (#1, Apr  7 2005, 11:06:30) [C] on osf1V5
Type help, copyright, credits or license for more information.
 execfile.__doc__
'execfile(filename[, globals[, locals]])\n\nRead and execute a Python script
from a file.\nThe globals and locals are dictionaries, defaulting to the
current\nglobals and locals.  If only globals is given, locals defaults to it.'
 

--- could ildg [EMAIL PROTECTED] wrote:

 I want to import c:\xxx\yyy\zzz.py into my programme,
 What should I do?
 Thank you~
 -- 
 http://mail.python.org/mailman/listinfo/python-list
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread Jesse Noller
A question in a similiar vein:

I have appended 2 different directories to my path (via
sys.path.append) now - without knowing the names of the files in those
directories, I want to force an import of the libraries ala:

for f in os.listdir(os.path.abspath(libdir)):
module_name = f.strip('.py')
import module_name

Obviously, this throws:

ImportError: No module named module_name

Is there some way to do this?

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


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread harold fellermann
 for f in os.listdir(os.path.abspath(libdir)):
 module_name = f.strip('.py')
 import module_name

 Obviously, this throws:

 ImportError: No module named module_name

 Is there some way to do this?

have a look at help(__import__) to import a module whose name is given 
as a string.

- harold -

--
Tages Arbeit, abends Gäste,
saure Wochen, frohe Feste!
-- Johann Wolfgang v. Goethe

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


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread Peter Hansen
Jesse Noller wrote:
 for f in os.listdir(os.path.abspath(libdir)):
 module_name = f.strip('.py')
 import module_name
 
 Obviously, this throws:
 ImportError: No module named module_name
 
 Is there some way to do this?

Use the __import__ builtin function.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread could ildg
for f in os.listdir(os.path.abspath(libdir)):
   module_name = f.strip('.py')
   __import__(module_name, globals(), locals(), [])

On 7/14/05, Jesse Noller [EMAIL PROTECTED] wrote:
 A question in a similiar vein:
 
 I have appended 2 different directories to my path (via
 sys.path.append) now - without knowing the names of the files in those
 directories, I want to force an import of the libraries ala:
 
 for f in os.listdir(os.path.abspath(libdir)):
 module_name = f.strip('.py')
 import module_name
 
 Obviously, this throws:
 
 ImportError: No module named module_name
 
 Is there some way to do this?
 
 thanks
 -jesse

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


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread Peter Hansen
Jesse Noller wrote:
 A question in a similiar vein:
 
 I have appended 2 different directories to my path (via
 sys.path.append) now - without knowing the names of the files in those
 directories, I want to force an import of the libraries ala:
 
 for f in os.listdir(os.path.abspath(libdir)):
 module_name = f.strip('.py')

This doesn't do what you think it does:

  'pyoopsyp.py'.strip('.py')
'oops'

Read the docs on strip(), and then the docs on os.path.splitext() and 
friends, and/or download and use Jasen Orendorrf's path module which 
will make all your path-related code half the size and twice as easy to 
read and write.

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


Re: How can I import a py script by its absolute path name?

2005-07-14 Thread could ildg
You are quite right~

On 7/15/05, Peter Hansen [EMAIL PROTECTED] wrote:
 Jesse Noller wrote:
  A question in a similiar vein:
 
  I have appended 2 different directories to my path (viaY
  sys.path.append) now - without knowing the names of the files in those
  directories, I want to force an import of the libraries ala:
 
  for f in os.listdir(os.path.abspath(libdir)):
  module_name = f.strip('.py')
 
 This doesn't do what you think it does:
 
   'pyoopsyp.py'.strip('.py')
 'oops'
 
 Read the docs on strip(), and then the docs on os.path.splitext() and
 friends, and/or download and use Jasen Orendorrf's path module which
 will make all your path-related code half the size and twice as easy to
 read and write.
 
 -Peter
 --
 http://mail.python.org/mailman/listinfo/python-list

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