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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • could ildg

    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~
  • Edvard Majakari

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

    could ildg <could.net@gmai l.com> writes:
    [color=blue]
    > I want to import c:\xxx\yyy\zzz. py into my programme,
    > What should I do?
    > Thank you~[/color]

    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!

    $_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
    join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";

    Comment

    • Thorsten Kampe

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

      * Edvard Majakari (2005-07-14 12:52 +0100)[color=blue]
      > could ildg <could.net@gmai l.com> writes:[color=green]
      >> I want to import c:\xxx\yyy\zzz. py into my programme,
      >> What should I do?
      >> Thank you~[/color]
      >
      > import sys
      > sys.path.append ('c:\xxx\yyy')[/color]

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

      Comment

      • Edvard Majakari

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

        Thorsten Kampe <thorsten@thors tenkampe.de> writes:

        [color=blue]
        > "sys.path.appen d('c:\\xxx\\yyy ')" or "sys.path.appen d('c:/xxx/yyy')"[/color]

        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,'6 e',2,64,3,76,7, 20,13,61,8,'4d' ,24,73,10,'6a', 12,'6b',21,68,1 4,
        72,16,'2c',17,2 0,9,61,11,61,25 ,74,4,61,1,45,2 9,20,5,72,18,61 ,15,69,20,43,26 ,
        69,19,20,6,64,2 7,61,22,72};$_= join'',map{chr hex $h->{$_}}sort{$a<= >$b}
        keys%$h;m/(\w).*\s(\w+)/x;$_.=uc substr(crypt(jo in('',60,28,14, 49),join'',
        map{lc}($1,subs tr $2,4,1)),2,4)." \n"; print;

        Comment

        • John Machin

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

          Thorsten Kampe wrote:[color=blue]
          > * Edvard Majakari (2005-07-14 12:52 +0100)
          >[color=green]
          >>could ildg <could.net@gmai l.com> writes:
          >>[color=darkred]
          >>>I want to import c:\xxx\yyy\zzz. py into my programme,
          >>>What should I do?
          >>>Thank you~[/color]
          >>
          >>import sys
          >>sys.path.appe nd('c:\xxx\yyy' )[/color]
          >
          >
          > "sys.path.appen d('c:\\xxx\\yyy ')" or "sys.path.appen d('c:/xxx/yyy')"[/color]

          or "sys.path.appen d(r'c:\xxx\yyy' )"

          Comment

          • Chris Lambacher

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

            You probably actually want:

            import sys
            sys.path.inster t(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:[color=blue]
            > could ildg <could.net@gmai l.com> writes:
            >[color=green]
            > > I want to import c:\xxx\yyy\zzz. py into my programme,
            > > What should I do?
            > > Thank you~[/color]
            >
            > 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!
            >
            > $_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
            > join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";
            > --
            > http://mail.python.org/mailman/listinfo/python-list[/color]

            Comment

            • could ildg

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

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

              On 7/14/05, Chris Lambacher <lambacck@compu ter.org> wrote:[color=blue]
              > You probably actually want:
              >
              > import sys
              > sys.path.inster t(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:[color=green]
              > > could ildg <could.net@gmai l.com> writes:
              > >[color=darkred]
              > > > I want to import c:\xxx\yyy\zzz. py into my programme,
              > > > What should I do?
              > > > Thank you~[/color]
              > >
              > > 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!
              > >
              > > $_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
              > > join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";
              > > --
              > > http://mail.python.org/mailman/listinfo/python-list[/color]
              > --
              > http://mail.python.org/mailman/listinfo/python-list
              >[/color]

              Comment

              • J.Bijsterbosch

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

                Hello Edward,

                "Edvard Majakari" <edvard+news@ma jakari.net> schreef in bericht
                news:87ackpeeag .fsf@titan.stas elog.com...[color=blue]
                > Thorsten Kampe <thorsten@thors tenkampe.de> writes:
                >[color=green]
                > > "sys.path.appen d('c:\\xxx\\yyy ')" or "sys.path.appen d('c:/xxx/yyy')"[/color]
                >
                > Well, of course. As I said, it was untested :) I just copied the path[/color]
                string,[color=blue]
                > and didn't remember Windows uses path names which need special
                > treatment.[/color]

                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


                Comment

                • Terry Hancock

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

                  On Thursday 14 July 2005 07:43 am, Thorsten Kampe wrote:[color=blue]
                  > * Edvard Majakari (2005-07-14 12:52 +0100)[color=green]
                  > > could ildg <could.net@gmai l.com> writes:[color=darkred]
                  > >> I want to import c:\xxx\yyy\zzz. py into my programme,
                  > >> What should I do?
                  > >> Thank you~[/color]
                  > >
                  > > import sys
                  > > sys.path.append ('c:\xxx\yyy')[/color]
                  >
                  > "sys.path.appen d('c:\\xxx\\yyy ')" or "sys.path.appen d('c:/xxx/yyy')"[/color]

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

                  my_mod = __import__('c:\ \xxx\\yyy\\mymo dule.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_subpackag e/module2.py"
                  has been suggested, and you can implement something like this using
                  the __import__ built-in as suggested above.

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


                  Comment

                  • James Dennett

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

                    J.Bijsterbosch wrote:
                    [color=blue]
                    > Hello Edward,
                    >
                    > "Edvard Majakari" <edvard+news@ma jakari.net> schreef in bericht
                    > news:87ackpeeag .fsf@titan.stas elog.com...
                    >[color=green]
                    >>Thorsten Kampe <thorsten@thors tenkampe.de> writes:
                    >>
                    >>[color=darkred]
                    >>>"sys.path.ap pend('c:\\xxx\\ yyy')" or "sys.path.appen d('c:/xxx/yyy')"[/color]
                    >>
                    >>Well, of course. As I said, it was untested :) I just copied the path[/color]
                    >
                    > string,
                    >[color=green]
                    >>and didn't remember Windows uses path names which need special
                    >>treatment.[/color]
                    >
                    >
                    > 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...;-))[/color]

                    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

                    Comment

                    • Edvard Majakari

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

                      "J.Bijsterbosch " <j.bijsterbosch @hccnet.nl> writes:
                      [color=blue]
                      > 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...;-))[/color]

                      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.

                      Comment

                      • J.Bijsterbosch

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

                        Hello James,

                        "James Dennett" <jdennett@acm.o rg> schreef in bericht
                        news:J7_Be.1482 4$HV1.4893@fed1 read07...[color=blue]
                        > J.Bijsterbosch wrote:
                        >[/color]
                        [ snip ][color=blue][color=green][color=darkred]
                        > >>and didn't remember Windows uses path names which need special
                        > >>treatment.[/color]
                        > >
                        > > Hmm, what you call special treatment<g> comes from pythons deep[/color][/color]
                        underlying C[color=blue][color=green]
                        > > and C++ language heietidge I presume. A backslash in a C or C++ string[/color][/color]
                        means[color=blue][color=green]
                        > > the following character is a so called escape character, like \n[/color][/color]
                        represents[color=blue][color=green]
                        > > 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...;-))[/color]
                        >
                        > 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.[/color]

                        I know<g>, I've had mandrake installed for some time until that pc died on
                        me, the pc that is, not mandrake...
                        [color=blue]
                        > 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.)[/color]

                        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...;-))
                        [color=blue]
                        > James[/color]

                        Greetings from overcast Amsterdam,

                        Jan


                        Comment

                        Working...