=> "Hello!
"
Modules can be imported by other modules.
# greet.py
import hello
hello.say_hello()
Specific functions of a module can be imported.
# greet.py
from hello import say_hello
say_hello()
Modules can be aliased.
# greet.py
import hello as ai
ai.say_hello()
A module can be stand-alone runnable script.
# run_hello.py
if __name__ == '__main__':
from hello import say_hello
say_hello()
Run it!
$ python run_hello.py
=> "Hello!"
If the module is inside a directory and needs to be detected by python, the directory should contain
a file named
__init__.py.
Section 1.10: Installation of Python 2.7.x and 3.x
Note: Following instructions are written for Python 2.7 (unless specified): instructions for Python 3.x
are
similar.
Windows
First, download the latest version of Python 2.7 from the official Website
(https://www.python.org/downloads/).
Version is provided as an MSI package. To install it manually, just double-click the file.
By default, Python installs to a directory:
C:\Python27\
Warning: installation does not automatically modify the PATH environment variable.
Assuming that your Python installation is in C:\Python27, add this to your PATH:
GoalKicker.com – Python® Notes for Professionals 27
C:\Python27\;C:\Python27\Scripts\
Now to check if Python installation is valid write in cmd:
python --version
Python 2.x and 3.x Side-By-Side
To install and use both Python 2.x and 3.x side-by-side on a Windows machine:
1. Install Python 2.x using the MSI installer.
Ensure Python is installed for all users.
Optional: add Python to PATH to make Python 2.x callable from the command-line using python.
2. Install Python 3.x using its respective installer.
Again, ensure Python is installed for all users.
Optional: add Python to PATH to make Python 3.x callable from the command-line using python. This
may override Python 2.x PATH settings, so double-check your PATH and ensure it's configured to your
preferences.
Make sure to install the py launcher for all users.
Python 3 will install the Python launcher which can be used to launch Python 2.x and Python 3.x
interchangeably
from the command-line:
P:\>py -3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\>py -2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
To use the corresponding version of pip for a specific Python version, use:
C:\>py -3 -m pip -V
pip 9.0.1 from C:\Python36\lib\site-packages (python 3.6)
C:\>py -2 -m pip -V
pip 9.0.1 from C:\Python27\lib\site-packages (python 2.7)
Linux
The latest versions of CentOS, Fedora, Red Hat Enterprise (RHEL) and Ubuntu come with Python 2.7.
To install Python 2.7 on linux manually, just do the following in terminal:
wget --no-check-certificate https://www.python.org/ftp/python/2.7.X/Python-2.7.X.tgz
tar -xzf Python-2.7.X.tgz
cd Python-2.7.X
./configure