473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

I want to import c:\xxx\yyy\zzz.py into my programme,
What should I do?
Thank you~
Jul 21 '05 #1
11 25022
could ildg <co*******@gmail.com> 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 available Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737 469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
Jul 21 '05 #2
* Edvard Majakari (2005-07-14 12:52 +0100)
could ildg <co*******@gmail.com> 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')"
Jul 21 '05 #3
Thorsten Kampe <th******@thorstenkampe.de> 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,7 3,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;
Jul 21 '05 #4
Thorsten Kampe wrote:
* Edvard Majakari (2005-07-14 12:52 +0100)
could ildg <co*******@gmail.com> 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')"
Jul 21 '05 #5
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 <co*******@gmail.com> 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 available Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737 469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
--
http://mail.python.org/mailman/listinfo/python-list

Jul 21 '05 #6
Thank you for your help.
It's really useful for me.

On 7/14/05, Chris Lambacher <la******@computer.org> 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 thatyou
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 <co*******@gmail.com> 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 available Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737 469616e20'; 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

Jul 21 '05 #7
Hello Edward,

"Edvard Majakari" <ed*********@majakari.net> schreef in bericht
news:87************@titan.staselog.com...
Thorsten Kampe <th******@thorstenkampe.de> 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 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
Jul 21 '05 #8
On Thursday 14 July 2005 07:43 am, Thorsten Kampe wrote:
* Edvard Majakari (2005-07-14 12:52 +0100)
could ildg <co*******@gmail.com> 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
Jul 21 '05 #9
J.Bijsterbosch wrote:
Hello Edward,

"Edvard Majakari" <ed*********@majakari.net> schreef in bericht
news:87************@titan.staselog.com...
Thorsten Kampe <th******@thorstenkampe.de> 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 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
Jul 21 '05 #10
"J.Bijsterbosch" <j.************@hccnet.nl> writes:
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 :)

--
# Edvard Majakari Software Engineer
# PGP PUBLIC KEY available Soli Deo Gloria!
You shouldn't verb verbs.
Jul 21 '05 #11
Hello James,

"James Dennett" <jd******@acm.org> schreef in bericht
news:J7_Be.14824$HV1.4893@fed1read07...
J.Bijsterbosch wrote:
[ snip ]
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...;-))


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 know<g>, 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
Jul 21 '05 #12

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

Similar topics

4
by: dchaffin | last post by:
I'm having a problem using file_exists with an absolute path and I can not figure out why. I tried the exact example that is on www.php.net ... <?php $filename = '/path/to/foo.txt'; if...
1
by: Alexander Hoffmann | last post by:
Hello, I have a project with a number of python modules, which means there are several *.py files all in the same folder /usr/local/myproject/*.py . I ran the program to test it and of course as...
7
by: Wayne | last post by:
I have a script that uses filesystemobject that reads files from a given path, in my case images. It is running on a server that is 2000 adv svr w/ all current patches. The script prior to some...
2
by: schaf | last post by:
Hello ! Im trying to access some XML Document by an absolute path with document() in my XSL-File. It looks like this: <xsl:param name="pData" as="xs:anyURI"/> <xsl:param name="sData"...
1
by: William Starr Moake | last post by:
Another problem with absolute paths in the WYSIWYG editor I'm putting together. The function to toggle between WYSIWYG and HTML modes works except for one glitch. If you use a relative path for...
7
by: Rizaan Jappie | last post by:
is it possible to get the relative path based on a absolute path in c#? *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
1
by: Kevin Liu | last post by:
Attribute Programming generates IDL file automatically. The IDL looks like import "C:\Program Files\Microsoft Visual Studio ..NET\Vc7\PlatformSDK\include\prsht.idl"; import "C:\Program...
3
by: PJ6 | last post by:
Is it possible to get the absolute path (i.e. C:\inetpub\wwwroot) of the IIS root directory? And yes, I do need to get the actual, absolute path, because the Crystal Reports XI web viewer does -...
27
by: kvnsmnsn | last post by:
I've written a piece of code that interfaces with Postgres. It needs to write a Postgres table to disk, which it does with the <COPYcom- mand. That command requires the absolute file name of the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.