Now, to test our package, navigate the command prompt to the MyApp folder and invoke the Python prompt from there. It serves two purposes:. You can optionally define functions from individual modules to be made available.
However, it can also be used to choose specific functions from modules in the package folder and make them available for import. The specified functions can now be imported in the interpreter session or another executable script. Note that functions power and SayHello are imported from the package and not from their respective modules, as done earlier.
The output of the above script is:. Once a package is created, it can be installed for system-wide use by running the setup script. The script calls setup function from the setuptools module. Save the following code as setup. The script calls the setup function from the setuptools module.
The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. If a package statement is not used then the class, interfaces, enumerations, and annotation types will be placed in the current default package. Then a folder with the given package name is created in the specified destination, and the compiled class files will be placed in that folder. Let us look at an example that creates a package called animals.
It is a good practice to use names of packages with lower case letters to avoid any conflicts with the names of classes and interfaces. If a class wants to use another class in the same package, the package name need not be used. Classes in the same package find each other without any special syntax. Here, a class named Boss is added to the payroll package that already contains Employee.
The Boss can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss class. What happens if the Employee class is not in the payroll package? The Boss class must then use one of the following techniques for referring to a class in a different package. The import statements must appear after the package statement and before the class declaration. The name of the package becomes a part of the name of the class, as we just discussed in the previous section.
The name of the package must match the directory structure where the corresponding bytecode resides. Packages in Java. Flow Control in Java. Loops in Java. Jump Statements in Java. Arrays in Java. Strings in Java. OOPS in Java. Constructors in Java. Interfaces in Java. Keywords in Java. Exception Handling in Java. Collection Framework. Multi-threading in Java.
Table of Contents. Improve Article. Save Article. Like Article. Vector is imported, hence we are. ArrayList is not imported, hence. ArrayList ;. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Previous Bitwise Operators in Java. Next Decision Making in Java if, if-else, switch, break, continue, jump. Recommended Articles. Article Contributed By :.
0コメント