Yahoo Web Search

Search results

  1. This lesson discusses modules and scripts. You’ll learn about the main differences between the two and you’ll see that: Scripts are top level files intended for execution and; Modules are intended to be imported

    • What's A script?
    • What's A Module?
    • Scripts Are Modules
    • Making A Script That Acts Nicely When Used as A Module
    • The Repl Is A Module!
    • Related Idea: Running Modules (and Scripts) Interactively

    A script or program is a .pyfile that's meant to be run directly. Here's an extremely simple script that prints out "Howdy user": If that file was called hello.py, we could run it from the command-line like this:

    A module is a .py file that's meant to be imported by other .pyfiles.Modules should contain helpful functions, classes, or variables pointing to useful data. Here's a module that has a single function, greet, within it: If this module was named salutations.py, we could import and use the greet function within it from the REPLlike this: Or we could ...

    All .py files are Python modules, even scripts.Scripts are modules that act as the entry pointto our Python process. You can prove to yourself that scripts are modules by importing a .pyfile that's meant to be used as a script: Importing a module runs all the code in that module and sticks it into a module object. You could even try running a modul...

    Most scripts aren't meantto be imported as modules (it's a bit odd to see something start happening simply because a module was imported). If you want to make a script that will print things out and do useful tasks when run from the command-line but won'tdo those things when imported, you'll need to somehow distinguish between two states: 1. Am I c...

    Interestingly, this __name__ thing even exists in the Python REPL. When you run python3 with no arguments, you're launching the Python REPL. Every Python process needs an entry point module. When you're in the REPL, you're creatingthat entry point module as you write code. As you type code at the REPL, you're basically writing a script on-the-fly. ...

    If you pass Python the -i argument as you run a script, Python will run all the code within your script and then it will drop you into a Python REPL that's insideyour module. Normally the REPL makes a new empty module for you to start writing your code in.When running a module interactively, the REPL will instead drop you into the module you gave i...

  2. Jun 8, 2010 · A script is generally a directly executable piece of code, run by itself. A module is generally a library, imported by other pieces of code.

  3. Oct 5, 2013 · A module is a single file of python code that is meant to be imported. This is a bit of a simplification since in practice quite a few modules detect when they are run as script and do something special in that case. A script is a single file of python code that is meant to be executed as the 'main' program.

  4. May 26, 2020 · A script is a Python file that’s intended to be run directly. When you run it, it should do something. This means that scripts will often contain code written outside the scope of any classes or functions. A module is a Python file that’s intended to be imported into scripts or other modules.

    • 5 min
  5. There are actually three different ways to define a module in Python: A module can be written in Python itself. A module can be written in C and loaded dynamically at run-time, like the re (regular expression) module. A built-in module is intrinsically contained in the interpreter, like the itertools module.

  6. People also ask

  7. 1 day ago · A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__.

  1. People also search for