C
could ildg
I want to import c:\xxx\yyy\zzz.py into my programme,
What should I do?
Thank you~
What should I do?
Thank you~
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')
"sys.path.append('c:\\xxx\\yyy')" or "sys.path.append('c:/xxx/yyy')"
Thorsten said:* Edvard Majakari (2005-07-14 12:52 +0100)
"sys.path.append('c:\\xxx\\yyy')" or "sys.path.append('c:/xxx/yyy')"
* Edvard Majakari (2005-07-14 12:52 +0100)
"sys.path.append('c:\\xxx\\yyy')" or "sys.path.append('c:/xxx/yyy')"
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.
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.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...;-))
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.)
James
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...;-))
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.