Skip to main content
Chemistry LibreTexts

6: User Created Package

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    This activity is correlated to Assignment 3.  In your class folder we want you to create a subfolder Sandbox and in it we want you to create a file called and subfolder called My_toolbox. and 

    So the directory hierarchy looks like this, where the folders start with a captial letter and the files all end with .py

    • Sandbox
      • Tools
        • __init.py__

    Python files

    This file is in the Sandbox and located next to the Tools directory (folder).  It will call functions from a module within the Tools package.  We will use the follow code for this.

    #Script 1:
    from Tools import my_toolbox as mtb
    num1=float(input("Enter first number: "))
    num2=float(input("Enter second number: "))
    print(f"The sum of {num1} and {num2} is {value}.")
    print(f"the type for the variable now is {type(value)}.\n")
    now = mtb.current_date()
    print(f"The current date is {now}.")
    print(f"the type for the variable now is {type(now)}.")


    Place this file in the Tools directory. The (and those are two underscores) is used to mark a directory as a Python Package and when present in a folder, python treats the folder as a package, which means it can be imported into another program as a module.  Right now we are going to keep the file empty, but for example, if the files in the package had dependencies on other packages, like the time module, they could be set up in the  file


    Place this file in the tools directory.  It will serve as a module where you can create functions (tools) that can be called from other programs. 

    #Script 2:
    '''This package contains functions that can be called by other programs'''
    def addition(num1,num2):
    def current_date():
        import datetime

    Note, there are two functions in the second script; addition and current_time. The addition has two arguments that must be submitted when the first script calls it (num1 and num2 are passed from script 1 to script 2 as arguments) and it returns one value, the sum.  The second function (current_time) has no parameters, and so you pass it nothing when you call it, but it returns the variable "today", which you assign to the variable "now" in the calling program




    6: User Created Package is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?