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

C

could ildg

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

Edvard Majakari

could ildg said:
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 available Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
 
T

Thorsten Kampe

* Edvard Majakari (2005-07-14 12:52 +0100)
import sys
sys.path.append('c:\xxx\yyy')

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

Edvard Majakari

"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;
 
J

John Machin

Thorsten said:
* Edvard Majakari (2005-07-14 12:52 +0100)



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

or "sys.path.append(r'c:\xxx\yyy')"
 
C

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
 
T

Terry Hancock

* Edvard Majakari (2005-07-14 12:52 +0100)

"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.
 
J

J.Bijsterbosch

Hello Edward,

Edvard Majakari said:
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 treatment<g> 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
 
C

could ildg

Thank you for your help.
It's really useful for me.

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
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 available Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
 
J

James Dennett

J.Bijsterbosch said:
Hello Edward,




Hmm, what you call special treatment<g> 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
 
J

J.Bijsterbosch

Hello James,

James Dennett said:
J.Bijsterbosch wrote:
[ snip ]
Hmm, what you call special treatment<g> 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've had mandrake installed for some time until that pc died on said:
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...;-))

Greetings from overcast Amsterdam,

Jan
 
E

Edvard Majakari

J.Bijsterbosch said:
Hmm, what you call special treatment<g> 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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top