Jun 17, 2017 This post shows how to add an entry to the Mac OS X PATH variable - (1) for the current terminal session only, (2) permanently for the current user only and (3) permanently for all users on a Mac OS X system. Here, the Tomcat startup.sh executable is used as an example. On Mac Osx terminal, if I simply write python, then it is going to python 2.7 and if I write python3.6 then it open up version 3.6. Is there anything I can do to point python to.
The import
statement is usually the first thing you see at the top of anyPython file. We use it all the time, yet it is still a bit mysterious tomany people. This tutorial will walk through how import works and howto view and modify the directories used for importing.
If you want to learn how to import a module by using a string variablename to reference the module, check out my tutorial on Import Python Module by String Name
Also check out my Python Virtual Environments Tutorial to learn moreabout isolated Python environments.
First, let's clarify the difference between modules and packages.They are very closely related, and often confused.They both serve the same purpose which is to organize code,but they each provide slightly different ways of doing that.
.py
file with Python code.A module can be thought of as a self-contained package, and a packageis like a module that is separated out across multiple files.It really depends on how you want to organize your code and how largeyour project is. I always start with a module and turn it in to a packageif needed later.
The import
keyword in Python is used to load other Python source code filesin to the current interpreter session. This is how you re-use code and share itamong multiple files or different projects.
There are a few different ways to use import
. For example, if we wantedto use the function join()
that lives in the path
module of the os
package.Its full name would be os.path.join()
. We have a few ways of importingand using the function. How to update icloud library on mac. Read more about the os
package athttps://docs.python.org/3/library/os.path.html.
There are a few different ways you can import a package or a module.You can directly call import
or use from x import y
format.The from
keyword tells Python what package or module to look infor the name specified with import
. Here are a few example that allaccomplish the same end goal.
Different ways to import and execute os.path.join()
:
As you can see, you can import the whole package, a specific module withina package, a specific function from within a module. The *
wildcard meansload all modules and functions. I do not recommend using the wildcard becauseit is too ambiguous. It is better to explicitly list each import so you canidentify where it came from. A good IDE like PyCharm will help you managethese easily.
When you call import
in the Python interpreter searches through a set ofdirectories for the name provided.The list of directories that it searches is stored in sys.path
and can bemodified during run-time. To modify the paths before starting Python,you can modify the PYTHONPATH
environment variable. Both sys.path
andPYTHONPATH
are covered more below.
If you want to import a module programmatically, you can use importlib.import_module()
.This function is useful if you are creating a plugin system where modulesneed to be loaded at run-time based on string names.
This method is not commonly used, and is only useful in special circumstances.For example, if you are building a plugin system where you want to load every filein a directory as a module based on the filepath string.
Names that start and end with double underscores, often called 'dunders',are special names in Python.Two of them are special names related to modules and packages: __init__
and __main__
.Depending on whether you are organizing your code as a package or a module,they are treated slightly differently.
We will look at the difference between a module and a package in a moment,but the main idea is this:
__init__.py
file inside the package directory.python -m my_package
) it executes the __main__.py
file.__name__
variable to the string '__main__'
.In a Python package (a directory), you can have a module named __init__.py
andanother named __main__.py
.
Here is an example structure of a package:
If a package is invoked directly (e.g. python -m my_package
), the __main__.py
module is executed.The __init__.py
file is executed when a package is imported (e.g. import my_package
).
In the previous section, we saw how a package can have separate files for __init__.py
and __main__.py
.In a module (a single .py file) the equivalent of __init__
and __main__
arecontained in the single file. The entire itself essentially becomes both the __init__.py
and the __main__.py
.
When a module is imported, it runs the whole file, loading any functions defined.
When a module is invoked directly, for example, python my_module.py
orpython -m my_module
, then it does the same thing as importing it, but alsosets the __name__
variable to the string '__main__'
.
You can take advantage of this and execute a section of code only ifthe module is invoked directly, and not when it is imported. To do this,you need to explicitly check the __name__
variable, and see if it equals __main__
.If it is set to the string __main__
, then you know the module was invokeddirectly, and not simply imported.
Take this example. Create a file named my_module.py
with the following contents:
Try out a few different things to understand how it works:
python my_module.py
-m
flag: python -m my_module
python -c 'import my_module'
python -c 'import my_module; my_module.my_function()'
When you start a Python interpreter, one of the things it creates automaticallyis a list that contains all of directories it will use to search for modules when importing.This list is available in a variable named sys.path
.Here is an example of printing out sys.path
.Note that the empty '
entry means the current directory.
You are allowed to modify sys.path
during run-time.Just be sure to modify it before you call import
.It will search the directories in order stopping at the first place itfinds the specified modules.
PYTHONPATH
is related to sys.path
very closely. PYTHONPATH
is anenvironment variable that you set before running the Python interpreter.PYTHONPATH
, if it exists, should contain directories that should be searchedfor modules when using import
. If PYTHONPATH
is set, Python will include the directories in sys.path
for searching.Use a semicolon to separate multiple directories.
Here is an example of setting the environment variable in Windows and listingthe paths in Python:
And in Linux and Mac you can do the equivalent like this:
So, in order to import modules or packages, they need to reside in one of the paths listed in sys.path
.You can modify the sys.path
list manually if needed from within Python.It is just a regular list
so it can be modified in all the normal ways.For example, you can append to the end of the list using sys.path.append()
or to insert in an arbitrary position using sys.path.insert()
.For more help, refer to https://docs.python.org/3/tutorial/datastructures.html
You can also use the site
module to modify sys.path
.See more at https://docs.python.org/3/library/site.html.
Example output:
You can also direclty invoke the site module to get a list of default paths:
Example run:
The PYTHONHOME
environment variable is similar to PYTHONPATH
except itshould define where the standard libraries are. If PYTHONHOME
is set,it will assume some default paths relative to the home, which can be supplemented with PYTHONPATH
.
This is particularly relevant if you embedded Python in to a C applicationand it is trying to determine the path of Python using the PYTHONHOME
environment variable.
Just for reference, here is a quick example of how you wouldbuild a C application with Python embedded in it.
Under the System Preferences, select iCloud.Step 2: Use your Apple ID and Password to sign in. The following steps should help you activate iCloud Photo Library on your Mac:Step 1: Open the Apple Menu and then select the System Preferences. How to access icloud photo library on my mac.
After reading this, you should have a better understanding of how Python'simport
statement works and how the PYTHONPATH
environment variableand sys.path
affect import
. You should also understand the differencesand similarities between modules and packages, the dunders __init__
and__main__
and how to use them effectively.
The os.path is another Python module, which also provides a big range of useful methods to manipulate files and directories. Most of the useful methods are listed here −
Sr.No. | Methods with Description |
---|---|
1 | os.path.abspath(path) Returns a normalized absolutized version of the pathname path. |
2 | os.path.basename(path) Returns the base name of pathname path. |
3 | os.path.commonprefix(list) Returns the longest path prefix (taken character-by-character) that is a prefix of all paths in list. |
4 | os.path.dirname(path) Returns the directory name of pathname path. |
5 | os.path.exists(path) Returns True if path refers to an existing path. Returns False for broken symbolic links. |
6 | os.path.lexists(path) Returns True if path refers to an existing path. Returns True for broken symbolic links. |
7 | os.path.expanduser(path) On Unix and Windows, returns the argument with an initial component of ~ or ~user replaced by that user's home directory. |
8 | os.path.expandvars(path) Returns the argument with environment variables expanded. |
9 | os.path.getatime(path) Returns the time of last access of path. |
10 | os.path.getmtime(path) Returns the time of last modification of path. |
11 | os.path.getctime(path) Returns the system's ctime, which on some systems (like Unix) is the time of the last change, and, on others (like Windows), is the creation time for path. |
12 | os.path.getsize(path) Returns the size, in bytes, of path. |
13 | os.path.isabs(path) Returns True if path is an absolute pathname. |
14 | os.path.isfile(path) Returns True if path is an existing regular file. |
15 | os.path.isdir(path) Returns True if path is an existing directory. |
16 | os.path.islink(path) Returns True if path refers to a directory entry that is a symbolic link. |
17 | os.path.ismount(path) Returns True if pathname path is a mount point: a point in a file system where a different file system has been mounted. |
18 | os.path.join(path1[, path2[, ..]]) Joins one or more path components intelligently. |
19 | os.path.normcase(path) Normalizes the case of a pathname. |
20 | os.path.normpath(path) Normalizes a pathname. |
21 | os.path.realpath(path) Returns the canonical path of the specified filename, eliminating any symbolic links encountered in the path |
22 | os.path.relpath(path[, start]) Returns a relative filepath to path either from the current directory or from an optional start point. |
23 | os.path.samefile(path1, path2) Returns True if both pathname arguments refer to the same file or directory |
24 | os.path.sameopenfile(fp1, fp2) Returns True if the file descriptors fp1 and fp2 refer to the same file. |
25 | os.path.samestat(stat1, stat2) Returns True if the stat tuples stat1 and stat2 refer to the same file. |
26 | os.path.split(path) Splits the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. |
27 | os.path.splitdrive(path) Splits the pathname path into a pair (drive, tail) where drive is either a drive specification or the empty string. |
28 | os.path.splitext(path) Splits the pathname path into a pair (root, ext) such that root + ext path, and ext is empty or begins with a period and contains at most one period. |
29 | os.path.splitunc(path) Splits the pathname path into a pair (unc, rest) so that unc is the UNC mount point (such as r'hostmount'), if present, and rest the rest of the path (such as r'pathfile.ext'). |
30 | os.path.walk(path, visit, arg) Calls the function visit with arguments (arg, dirname, names) for each directory in the directory tree rooted at path (including path itself, if it is a directory). |