It’s really a troublesome problem.
Splitting code into different files is really important for a large project. But when I tried to modulize code in Python, I find it’s really troublesome and made me a headache. Here are what I learned.
Import a file in the same directory
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given two text files, the task is to write a Python program to copy contents of the first file into the second file.
The text files which are going to be used are second.txt and first.txt:
Method #1: UsingFile handling to read and append
We will open first.txt in ‘r’ modeand will read the contents of first.txt. After that, we will open second.txt in ‘a’ mode and will append the content of first.txt into second.txt.
Example:
Python3
with
open
[
'first.txt'
,
'r'
] as firstfile,
open
[
'second.txt'
,
'a'
] as secondfile:
for
line
in
firstfile:
secondfile.write[line]
Output:
Method #2: UsingFile handling to read and write
We will open first.txt in ‘r’ modeand will read the contents of first.txt. After that, we will open second.txt in ‘w’ mode and will write the content of first.txt into second.txt.
Example:
Python3
with
open
[
'first.txt'
,
'r'
] as firstfile,
open
[
'second.txt'
,
'w'
] as secondfile:
for
line
in
firstfile:
secondfile.write[line]
Output:
Method #3: Using shutil.copy[] module
The shutil.copy[] method in Python is used to copy the content of the source file to destination file or directory.
Example:
Python3
import
shutil
shutil.copyfile[
'first.txt'
,
'second.txt'
]
Output:
In Python, a module is a single unit of Python code that can be imported [loaded and used] by other Python code. A module can contain definitions [like functions and constants], as well as statements that initialize those definitions. Once the module code is written, it can be reused by any script that imports the module.
A common way to create a Python module is to create a file with a filename that ends in .py
, and write the module code in there.
If we use a different file extension in the filename, or no extension at all, the standard import
statements shown below will not work, and we'll have to use importlib
instead, which requires more code [more info below].
To illustrate standard import
usage, let's say we create a file called mymodule.py
with the following function definition:
Now
every time we want to write "Hello, world!" to the screen from a Python script, we can simply import this module rather than having to write the message again. It also allows us to change one line of code inside mymodule.py
rather than in many different scripts if we ever decide to change the message we want to show in all the scripts that use this function.
mymodule.py
contains the say_hello[]
function we saw above.
The name used in the import
statement is simply the module's filename
without the .py
extension at the end.
Python versions 3.3 and higher allow easy imports of modules in subdirectories of the current script's directory. If you're using a Python version lower than 3.3, you can follow the steps in Import a File in a Different Directory instead.
In a file system path, we would separate the components of a path using /
[Linux,
macOS, etc.] or \
[Windows]. In a Python import
statement, however, we separate the path components using a dot [.
].
where m
is any name we choose. We can also import the function directly:
This works even if there are multiple levels of subdirectories. For example, if we had the following directory structure:
Now let's say that we move mymodule.py
to a directory that is outside of the current directory tree:
By default, Python looks for files in the
same directory [including subdirectories] as the current script file, as well as in other standard locations defined in sys.path
. If you're curious what these locations are, you can print out the sys.path
variable like this:
However, if the file we want to import is somewhere else entirely, we'll first have to tell Python where to look by adding search directories to sys.path
. In our example, we can write in script.py
:
Note that the path appended to sys.path
is an absolute path. If we
used a relative path, the path would resolve differently based on the directory from which the user is running the script, not relative to script.py
's path.
To append a directory relative to this script file, you can use __file__
to get the current script's full path and build a full path to the import from there. In script.py
we can write:
Python versions 3.4 and higher provide functionality through the built-in importlib
library that allows us to load any file anywhere as a
Python module, even if the file's filename does not end in .py
[it can have a different file extension, or no file extension at all].
Notice here that the mymodule
filename does not have a file extension. In this case, we can't use a simple import
statement to import that file. Instead, we can write in script.py
:
Note that the path passed into SourceFileLoader[]
is an absolute path. If we used a relative path like ../alpha/beta/mymodule
, the path would resolve differently based on the directory from
which the user is running the script, not relative to script.py
's path.
If we want to reference a file relative to our current script file's path, we can use __file__
to first get our current script file's path, and then build a full path from there:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import importlib.machinery import importlib.util from pathlib import Path # Get path to mymodule script_dir = Path[ __file__ ].parent mymodule_path = str[ script_dir.joinpath[ '..', 'alpha', 'beta', 'mymodule' ] ] # Import mymodule loader = importlib.machinery.SourceFileLoader[ 'mymodule', mymodule_path ] spec = importlib.util.spec_from_loader[ 'mymodule', loader ] mymodule = importlib.util.module_from_spec[ spec ] loader.exec_module[ mymodule ] # Use mymodule mymodule.say_hello[] |