- Installing Python Modules¶ Email. Distutils-sig @ python. As a popular open source development project, Python has an active supporting community of contributors and users that also make their software available for other Python developers to use under open source license terms.
- These built-in functions, however, are limited, and we can make use of modules to make more sophisticated programs. In Python, modules are accessed by using the import statement. When you do this, you execute the code of the module, keeping the scopes of the definitions so that your current file(s.
Gpg -verify Python-3.6.2.tgz.asc Note that you must use the name of the signature file, and you should use the one that's appropriate to the download you're verifying. (These instructions are geared to GnuPG and Unix command-line users.) Other Useful Items. Looking for 3rd party Python modules? The Package Index has many of them. Unofficial Windows Binaries for Python Extension Packages. By Christoph Gohlke, Laboratory for Fluorescence Dynamics, University of California, Irvine. Updated on 25 September 2019 at 20:16 UTC. This page provides 32- and 64-bit Windows binaries of many scientific open-source extension packages for the official CPython distribution of the Python programming language. Installing Packages¶. This section covers the basics of how to install Python packages. It’s important to note that the term “package” in this context is being used as a synonym for a distribution (i.e. A bundle of software to be installed), not to refer to the kind of package that you import in your Python source code (i.e. A container of modules).
Related
How To Add Stimulus to a Ruby on Rails Application Tutorial
Customizing Go Binaries with Build Tags Tutorial
Introduction
The Python programming language comes with a variety of built-in functions. Among these are several common functions, including:
print()
which prints expressions outabs()
which returns the absolute value of a numberint()
which converts another data type to an integerlen()
which returns the length of a sequence or collection
These built-in functions, however, are limited, and we can make use of modules to make more sophisticated programs.
Modules are Python
.py
files that consist of Python code. Any Python file can be referenced as a module. A Python file called hello.py
has the module name of hello
that can be imported into other Python files or used on the Python command line interpreter. You can learn about creating your own modules by reading How To Write Modules in Python 3.Modules can define functions, classes, and variables that you can reference in other Python
.py
files or via the Python command line interpreter. In Python, modules are accessed by using the
import
statement. When you do this, you execute the code of the module, keeping the scopes of the definitions so that your current file(s) can make use of these.When Python imports a module called
hello
for example, the interpreter will first search for a built-in module called hello
. If a built-in module is not found, the Python interpreter will then search for a file named hello.py
in a list of directories that it receives from the sys.path
variable.This tutorial will walk you through checking for and installing modules, importing modules, and aliasing modules.
Checking For and Installing Modules
There are a number of modules that are built into the Python Standard Library, which contains many modules that provide access to system functionality or provide standardized solutions. The Python Standard Library is part of every Python installation.
To check that these Python modules are ready to go, enter into your local Python 3 programming environment or server-based programming environment and start the Python interpreter in your command line like so:
From within the interpreter you can run the
import
statement to make sure that the given module is ready to be called, as in:Since
math
is a built-in module, your interpreter should complete the task with no feedback, returning to the prompt. This means you don’t need to do anything to start using the math
module. Let’s run the
import
statement with a module that you may not have installed, like the 2D plotting library matplotlib
:If
matplotlib
is not installed, you’ll receive an error like this:You can deactivate the Python interpreter with
CTRL + D
and then install matplotlib
with pip
.Next, we can use
pip
to install the matplotlib
module:Once it is installed, you can import
matplotlib
in the Python interpreter using import matplotlib
, and it will complete without error.Importing Modules
To make use of the functions in a module, you’ll need to import the module with an
import
statement. An
import
statement is made up of the import
keyword along with the name of the module.In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.
So, in the Python program file
my_rand_int.py
we would import the random
module to generate random numbers in this manner:When we import a module, we are making it available to us in our current program as a separate namespace. This means that we will have to refer to the function in dot notation, as in
[module].[function]
. In practice, with the example of the
random
module, this may look like a function such as:random.randint()
which calls the function to return a random integer, orrandom.randrange()
which calls the function to return a random element from a specified range.
Let’s create a
for
loop to show how we will call a function of the random
module within our my_rand_int.py
program:my_rand_int.py
This small program first imports the
random
module on the first line, then moves into a for
loop which will be working with 10 elements. Within the loop, the program will print a random integer within the range of 1 through 25 (inclusive). The integers 1
and 25
are passed to random.randint()
as its parameters.When we run the program with
python my_rand_int.py
, we’ll receive 10 random integers as output. Because these are random you’ll likely get different integers each time you run the program, but they’ll look something like this:The integers should never go below 1 or above 25.
If you would like to use functions from more than one module, you can do so by adding multiple
import
statements:You may see programs that import multiple modules with commas separating them — as in
import random, math
— but this is not consistent with the PEP 8 Style Guide.To make use of our additional module, we can add the constant
pi
from math
to our program, and decrease the number of random integers printed out:my_rand_int.py
Now, when we run our program, we’ll receive output that looks like this, with an approximation of pi as our last line of output:
The
import
statement allows you to import one or more modules into your Python program, letting you make use of the definitions constructed in those modules.Using from
… import
To refer to items from a module within your program’s namespace, you can use the
from
… import
statement. When you import modules this way, you can refer to the functions by name rather than through dot notationIn this construction, you can specify which definitions to reference directly.
In other programs, you may see the
import
statement take in references to everything defined within the module by using an asterisk (*
) as a wildcard, but this is discouraged by PEP 8.Let’s first look at importing one specific function,
randint()
from the random
module:Here, we first call the
from
keyword, then random
for the module. Next, we use the import
keyword and call the specific function we would like to use.Now, when we implement this function within our program, we will no longer write the function in dot notation as
random.randint()
but instead will just write randint()
:my_rand_int.py
When you run the program, you’ll receive output similar to what we received earlier.
Using the
from
… import
construction allows us to reference the defined elements of a module within our program’s namespace, letting us avoid dot notation.Aliasing Modules
It is possible to modify the names of modules and their functions within Python by using the
as
keyword.You may want to change a name because you have already used the same name for something else in your program, another module you have imported also uses that name, or you may want to abbreviate a longer name that you are using a lot.
The construction of this statement looks like this:
Let’s modify the name of the
math
module in our my_math.py
program file. We’ll change the module name of math
to m
in order to abbreviate it. Our modified program will look like this:Within the program, we now refer to the
pi
constant as m.pi
rather than math.pi
. For some modules, it is commonplace to use aliases. The
matplotlib.pyplot
module’s official documentation calls for use of plt
as an alias:This allows programmers to append the shorter word
plt
to any of the functions available within the module, as in plt.show()
. You can see this alias import statement in use within our “How to Plot Data in Python 3 Using matplotlib
tutorial.”Conclusion
When we import modules we’re able to call functions that are not built into Python. Some modules are installed as part of Python, and some we will install through
pip
.Making use of modules allows us to make our programs more robust and powerful as we’re leveraging existing code. We can also create our own modules for ourselves and for other programmers to use in future programs.
- Python Basic Tutorial
- Python Advanced Tutorial
- Python Useful Resources
- Selected Reading
A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.
![Free Free](/uploads/1/2/4/8/124862024/196829091.png)
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.
Example
![Pip not working python 3.7 Pip not working python 3.7](/uploads/1/2/4/8/124862024/932361872.jpg)
The Python code for a module named aname normally resides in a file named aname.py. Here's an example of a simple module, support.py
The import Statement
You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax −
When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, you need to put the following command at the top of the script −
When the above code is executed, it produces the following result −
A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again if multiple imports occur.
The from...import Statement
Python's from statement lets you import specific attributes from a module into the current namespace. The from...import has the following syntax −
For example, to import the function fibonacci from the module fib, use the following statement −
This statement does not import the entire module fib into the current namespace; it just introduces the item fibonacci from the module fib into the global symbol table of the importing module.
The from...import * Statement
It is also possible to import all names from a module into the current namespace by using the following import statement −
This provides an easy way to import all the items from a module into the current namespace; however, this statement should be used sparingly.
Locating Modules
When you import a module, the Python interpreter searches for the module in the following sequences −
- The current directory.
- If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
- If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.
The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.
The PYTHONPATH Variable
The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH.
Here is a typical PYTHONPATH from a Windows system −
And here is a typical PYTHONPATH from a UNIX system −
Namespaces and Scoping
Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys) and their corresponding objects (values).
A Python statement can access variables in a local namespace and in the global namespace. If a local and a global variable have the same name, the local variable shadows the global variable.
Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions.
Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local.
Therefore, in order to assign a value to a global variable within a function, you must first use the global statement.
The statement global VarName tells Python that VarName is a global variable. Python stops searching the local namespace for the variable.
For example, we define a variable Money in the global namespace. Within the function Money, we assign Money a value, therefore Python assumes Money as a local variable. However, we accessed the value of the local variable Money before setting it, so an UnboundLocalError is the result. Uncommenting the global statement fixes the problem.
The dir( ) Function
The dir() built-in function returns a sorted list of strings containing the names defined by a module.
The list contains the names of all the modules, variables and functions that are defined in a module. Following is a simple example −
When the above code is executed, it produces the following result −
Here, the special string variable __name__ is the module's name, and __file__ is the filename from which the module was loaded.
The globals() and locals() Functions
The globals() and locals() functions can be used to return the names in the global and local namespaces depending on the location from where they are called.
If locals() is called from within a function, it will return all the names that can be accessed locally from that function.
If globals() is called from within a function, it will return all the names that can be accessed globally from that function.
The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.
The reload() Function
When the module is imported into a script, the code in the top-level portion of a module is executed only once.
Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function. The reload() function imports a previously imported module again. The syntax of the reload() function is this −
Here, module_name is the name of the module you want to reload and not the string containing the module name. For example, to reload hello module, do the following −
Packages in Python
A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages, and so on.
Consider a file Pots.py available in Phone directory. This file has following line of source code −
Similar way, we have another two files having different functions with the same name as above −
Download Python Modules Mac
- Phone/Isdn.py file having function Isdn()
- Phone/G3.py file having function G3()
Now, create one more file __init__.py in Phone directory −
- Phone/__init__.py
To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows −
Python Modules Location
After you add these lines to __init__.py, you have all of these classes available when you import the Phone package.
When the above code is executed, it produces the following result −
Python Modules List
In the above example, we have taken example of a single functions in each file, but you can keep multiple functions in your files. You can also define different Python classes in those files and then you can create your packages out of those classes.