import

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

    import

    i have written script save as hello.py. i can run it. but why cant i
    import it as a modular in other programs?

  • bugnthecode

    #2
    Re: import

    How are you trying to import it? Is it in the same directory as your
    other script? If not is your python path set correctly?

    When importing a module that you have written you exlude the .py
    extension. You should be using:
    import hello

    Hope that helps,
    Will

    Comment

    • figo_wei01@126.com

      #3
      Re: import


      bugnthecode 写道:
      How are you trying to import it? Is it in the same directory as your
      other script? If not is your python path set correctly?
      >
      When importing a module that you have written you exlude the .py
      extension. You should be using:
      import hello
      >
      Hope that helps,
      Will
      i am on a windows platform. i have written scrip named 123.py. it can
      be run. ok i save it to C:\Python24 ,exactly the same dir where python
      works. but " import 123" doesnt work.

      Comment

      • resnak

        #4
        Re: import

        Try putting it in C:\Python24\Lib \site-packages instead.

        figo_wei01@126. com wrote:
        >bugnthecode 写道:
        >
        >How are you trying to import it? Is it in the same directory as your
        >other script? If not is your python path set correctly?
        >>
        >When importing a module that you have written you exlude the .py
        >extension. You should be using:
        >import hello
        >>
        >Hope that helps,
        >Will
        >
        >i am on a windows platform. i have written scrip named 123.py. it can
        >be run. ok i save it to C:\Python24 ,exactly the same dir where python
        >works. but " import 123" doesnt work.

        Comment

        • 01

          #5
          Re: import


          resnak wrote:
          Try putting it in C:\Python24\Lib \site-packages instead.
          i have done that , dest not work as well.

          Comment

          • Georg Brandl

            #6
            Re: import

            figo_wei01@126. com wrote:
            bugnthecode 写道:
            >
            >How are you trying to import it? Is it in the same directory as your
            >other script? If not is your python path set correctly?
            >>
            >When importing a module that you have written you exlude the .py
            >extension. You should be using:
            >import hello
            >>
            >Hope that helps,
            >Will
            >
            i am on a windows platform. i have written scrip named 123.py. it can
            be run. ok i save it to C:\Python24 ,exactly the same dir where python
            works. but " import 123" doesnt work.
            You can't import modules whose names have non-identifier names with
            plain "import". Or would you like "123" to refer to a module?

            If you have to do this (and I have a strong feeling that you haven't)
            use the built-in function __import__().

            Georg

            Comment

            • elliot.hughes@gmail.com

              #7
              Re: import

              If your still having trouble how are you running python? If you run cmd
              and THEN type python it is not operating in the python24 directory and
              runs in the directory cmd was in before the command. Otherwise make
              sure you don't have any previous python installs.

              Comment

              • 01

                #8
                Re: import


                Georg Brandl wrote:
                figo_wei01@126. com wrote:
                bugnthecode 写道:
                How are you trying to import it? Is it in the same directory as your
                other script? If not is your python path set correctly?
                >
                When importing a module that you have written you exlude the .py
                extension. You should be using:
                import hello
                >
                Hope that helps,
                Will
                i am on a windows platform. i have written scrip named 123.py. it can
                be run. ok i save it to C:\Python24 ,exactly the same dir where python
                works. but " import 123" doesnt work.
                >
                You can't import modules whose names have non-identifier names with
                plain "import". Or would you like "123" to refer to a module?
                >
                If you have to do this (and I have a strong feeling that you haven't)
                use the built-in function __import__().
                >
                you have to name your program with the name mymodule,or something like
                that when you use "import". does python have a clear declaration on
                how to name a module?

                Comment

                • Georg Brandl

                  #9
                  Re: import

                  01 wrote:
                  Georg Brandl wrote:
                  >figo_wei01@126. com wrote:
                  bugnthecode 写道:
                  >
                  >How are you trying to import it? Is it in the same directory as your
                  >other script? If not is your python path set correctly?
                  >>
                  >When importing a module that you have written you exlude the .py
                  >extension. You should be using:
                  >import hello
                  >>
                  >Hope that helps,
                  >Will
                  >
                  i am on a windows platform. i have written scrip named 123.py. it can
                  be run. ok i save it to C:\Python24 ,exactly the same dir where python
                  works. but " import 123" doesnt work.
                  >>
                  >You can't import modules whose names have non-identifier names with
                  >plain "import". Or would you like "123" to refer to a module?
                  >>
                  >If you have to do this (and I have a strong feeling that you haven't)
                  >use the built-in function __import__().
                  >>
                  you have to name your program with the name mymodule,or something like
                  that when you use "import". does python have a clear declaration on
                  how to name a module?
                  A module name can be everything also usable as a Python identifier. This
                  means that it contains only letters, digits and underscores and doesn't
                  start with a digit.

                  Running a file as the main file, using "python 123.py" works because
                  Python then won't need to provide the file name as a name in the script.

                  Georg

                  Comment

                  • Bill Pursell

                    #10
                    Re: import


                    Georg Brandl wrote:
                    01 wrote:
                    Georg Brandl wrote:
                    figo_wei01@126. com wrote:
                    bugnthecode 写道:

                    How are you trying to import it? Is it in the same directory as your
                    other script? If not is your python path set correctly?
                    >
                    When importing a module that you have written you exlude the .py
                    extension. You should be using:
                    import hello
                    >
                    Hope that helps,
                    Will

                    i am on a windows platform. i have written scrip named 123.py. it can
                    be run. ok i save it to C:\Python24 ,exactly the same dir where python
                    works. but " import 123" doesnt work.
                    >
                    You can't import modules whose names have non-identifier names with
                    plain "import". Or would you like "123" to refer to a module?
                    >
                    If you have to do this (and I have a strong feeling that you haven't)
                    use the built-in function __import__().
                    >
                    you have to name your program with the name mymodule,or something like
                    that when you use "import". does python have a clear declaration on
                    how to name a module?
                    >
                    A module name can be everything also usable as a Python identifier. This
                    means that it contains only letters, digits and underscores and doesn't
                    start with a digit.
                    >
                    Running a file as the main file, using "python 123.py" works because
                    Python then won't need to provide the file name as a name in the script.
                    On an almost totally unrelated note...(except that it definitely
                    belongs in a thread entitled "import").. .

                    I've occasionally run into difficulty when I fail to include a she-bang
                    in my script. If the first word of the script is import, which it
                    often is for quickly written scripts that contain an import line
                    before any doc string and that doesn't have "#!/usr/bin/env python",
                    bash doesn't report a syntax error. Instead, it annoyingly
                    runs the ImageMagick command import, which dutifully sits
                    there waiting for a mouse click. The first time this
                    happened, it took me a good 15 minutes to figure out
                    what was happening. (The first instance was more subtle
                    than forgetting the she-bang, as I'd merely forgotten
                    the "bang" and the "#/usr/bin/env python" looked pretty
                    good when I looked at the file.)

                    I'm just curious if anyone else has ever bumped into
                    that mistake.

                    Comment

                    Working...