Python module hello world

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Python Hello World module to create an egg file

ganeshchand/python-hello-world

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

##You can use this repository to create a python egg file which can then be used to test for libration installation.

  • Python 2.7.x
  • Setuptools — build tool to create python egg file
  • easy_install — python library install tool

Note: This git repository already includes hello-1.0-py2.7.egg located under dist folder.

You can simply download this file on your local machine and install it in you python environment using

###Using the HelloWorld module

from hello import helloworld print helloworld()

###Steps to create a python egg file:

  • git clone https://github.com/ganeshchand/python-hello-world.git
  • cd python-helloworld
  • python setup.py bdist_egg . This will create hello-1.0-py2.7.egg file under dist folder
  • Below is the directory structure
python-hello-world/ | build | dist/ | hello-1.0-py2.7.egg hello/ | __init__.py | hello.py | hello.egg-info | setup.py 

where build , dist and hello.egg-info artifacts are generated as part of the build process.

hello is a python package — a module that contains other modules; typically contained in a directory in the filesystem and distinguished from other directories by the presence of a file init.py. hello.py is a python module containg the source code.

__init__.py is required to make Python treat the directory as a python package

setup.py used to create python egg file

Источник

How To Write Modules in Python 3

Python modules are .py files that consist of Python code. Any Python file can be referenced as a module.

Some modules are available through the Python Standard Library and are therefore installed with your Python installation. Others can be installed with Python’s package manager pip . Additionally, you can create your own Python modules since modules are comprised of Python .py files.

This tutorial will guide you through writing Python modules for use within other programming files.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Writing and Importing Modules

Writing a module is like writing any other Python file. Modules can contain definitions of functions, classes, and variables that can then be utilized in other Python programs.

Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the python3 command. Then you can copy, paste, or edit the examples by adding them after the >>> prompt.

From our Python 3 local programming environment or server-based programming environment, let’s start by creating a file hello.py that we’ll later import into another file.

To begin, we’ll create a function that prints Hello, World! :

# Define a function def world(): print("Hello, World!") 

If we run the program on the command line with python hello.py nothing will happen since we have not told the program to do anything.

Let’s create a second file in the same directory called main_program.py so that we can import the module we just created, and then call the function. This file needs to be in the same directory so that Python knows where to find the module since it’s not a built-in module.

# Import hello module import hello # Call function hello.world() 

Because we are importing a module, we need to call the function by referencing the module name in dot notation.

We could instead import the module as from hello import world and call the function directly as world() . You can learn more about this method by reading how to using from … import when importing modules.

Now, we can run the program on the command line:

When we do, we’ll receive the following output:

To see how we can use variables in a module, let’s add a variable definition in our hello.py file:

# Define a function def world(): print("Hello, World!") # Define a variable shark = "Sammy" 

Next, we’ll call the variable in a print() function within our main_program.py file:

# Import hello module import hello # Call function hello.world() # Print variable print(hello.shark) 

Once we run the program again, we’ll receive the following output:

Finally, let’s also define a class in the hello.py file. We’ll create the class Octopus with name and color attributes and a function that will print out the attributes when called.

# Define a function def world(): print("Hello, World!") # Define a variable shark = "Sammy" # Define a class class Octopus: def __init__(self, name, color): self.color = color self.name = name def tell_me_about_the_octopus(self): print("This octopus is " + self.color + ".") print(self.name + " is the octopus's name.") 

We’ll now add the class to the end of our main_program.py file:

# Import hello module import hello # Call function hello.world() # Print variable print(hello.shark) # Call class jesse = hello.Octopus("Jesse", "orange") jesse.tell_me_about_the_octopus() 

Once we have called the Octopus class with hello.Octopus() , we can access the functions and attributes of the class within the main_program.py file’s namespace. This lets us write jesse.tell_me_about_the_octopus() on the last line without invoking hello . We could also, for example, call one of the class’s attributes such as jesse.color without referencing the name of the hello module.

When we run the program, we’ll receive the following output:

Output
Hello, World! Sammy This octopus is orange. Jesse is the octopus's name.

It is important to keep in mind that though modules are often definitions, they can also implement code. To see how this works, let’s rewrite our hello.py file so that it implements the world() function:

# Define a function def world(): print("Hello, World!") # Call function within module world() 

We have also deleted the other definitions in the file.

Now, in our main_program.py file, we’ll delete every line except for the import statement:

# Import hello module import hello 

When we run main_program.py we’ll receive the following output:

This is because the hello module implemented the world() function which is then passed to main_program.py and executes when main_program.py runs.

A module is a Python program file composed of definitions or code that you can leverage in other Python program files.

Accessing Modules from Another Directory

Modules may be useful for more than one programming project, and in that case it makes less sense to keep a module in a particular directory that’s tied to a specific project.

If you want to use a Python module from a location other than the same directory where your main program is, you have a few options.

Appending Paths

One option is to invoke the path of the module via the programming files that use that module. This should be considered more of a temporary solution that can be done during the development process as it does not make the module available system-wide.

To append the path of a module to another programming file, you’ll start by importing the sys module alongside any other modules you wish to use in your main program file.

The sys module is part of the Python Standard Library and provides system-specific parameters and functions that you can use in your program to set the path of the module you wish to implement.

For example, let’s say we moved the hello.py file and it is now on the path /usr/ sammy / while the main_program.py file is in another directory.

In our main_program.py file, we can still import the hello module by importing the sys module and then appending /usr/ sammy / to the path that Python checks for files.

import sys sys.path.append('/usr/sammy/') import hello ... 

As long as you correctly set the path for the hello.py file, you’ll be able to run the main_program.py file without any errors and receive the same output as above when hello.py was in the same directory.

Adding the Module to the Python Path

A second option that you have is to add the module to the path where Python checks for modules and packages. This is a more permanent solution that makes the module available environment-wide or system-wide, making this method more portable.

To find out what path Python checks, run the Python interpreter from your programming environment:

Next, import the sys module:

Then have Python print out the system path:

Here, you’ll receive some output with at least one system path. If you’re in a programming environment, you may receive several. You’ll want to look for the one that is in the environment you’re currently using, but you may also want to add the module to your main system Python path. What you’re looking for will be similar to this:

Output
'/usr/sammy/my_env/lib/python3.5/site-packages'

Now you can move your hello.py file into that directory. Once that is complete, you can import the hello module as usual:

When you run your program, it should complete without error.

Modifying the path of your module can ensure that you can access the module regardless of what directory you are in. This is useful especially if you have more than one project referencing a particular module.

Conclusion

Writing a Python module is the same as writing any other Python .py file. This tutorial covered how to write definitions within a module, make use of those definitions within another Python programming file, and went over options of where to keep the module in order to access it.

You can learn more about installing and importing modules by reading How To Import Modules in Python 3.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Tutorial Series: How To Code in Python

Python banner image

Introduction

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Источник

Читайте также:  Только на css таб вкладки
Оцените статью