Python import one file

How to Import a File in Python

To import a file in Python, you can use the “import statement”, which makes the module’s contents available in your current script. The import statement combines two operations; it searches for the named module, then binds the search results to a name in the local scope.

Python can put definitions in one file and use them in the script or an interactive instance of the interpreter. Such a file is called the module; definitions from a module can be imported into other modules or the main module (the collection of variables you can access in the script executed at the top level and in calculator mode).

The file name is a module name with the suffix .py appended. The module’s name (as a string) is available as the value of the global variable __name__.

Python import syntax

Example

Create a new file called sum.py and add the following code inside the file.

def add(a, b): c = a + b return c

The sum.py file has one function called add(), which takes two parameters and returns the sum of the provided arguments.

The sum.py file is a module in the above file, and add() is its method.

Читайте также:  Java oracle date string

We can import the sum module using the “import sum” inside the other file. But first, let’s import the app.py file, which is in the same directory as the sum.py file.

So, the import syntax is the following.

In our example, modulename = sum.

Now, we can use the add() function of the sum module.

# app.py import sum print(sum.add(3, 4))

If you intend to use the add() function more than once in your file, you can assign it to a local name.

# app.py import sum summation = sum.add print(summation(3, 4)) 

In the above code, we have assigned the sum() function to summation the local name and then use it anywhere in the file.

2 thoughts on “How to Import a File in Python”

I must remove “-” and “_” from the name of the file to import; from “Coef-Binomiales23.py” to “CoefBinomiales.py” in order that can be imported. CoefBinomiales23
from math import factorial
def Comb(n, k):
c = factorial(n)/(factorial(n – k)*factorial(k)) Llama-Funcion.py
a = int(input(“Dame valor grande = “))
b = int(input(“Dame valor chico = “))
y = CoefBinomiales23.Comb(a, b)
print(“La combinación combinada es = “, y) Only when I remove – and _ from the names the importation can be done, despite they are allowed in varible names. Reply

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Python 3: Import Another Python File as a Module

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:

def say_hello(): print( 'Hello, world!' )

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.

Import a File in the Same Directory

Let’s say we have two Python files in the same directory:

mymodule.py contains the say_hello() function we saw above.

To call say_hello() from inside script.py , we can write in script.py :

import mymodule mymodule.say_hello()

The name used in the import statement is simply the module’s filename without the .py extension at the end.

Import a File in a Subdirectory (Python 3.3 and Up)

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.

Let’s say we move mymodule.py to a subdirectory called subdir :

Then if we’re using Python 3.3 or higher, we can write in script.py :

import subdir.mymodule subdir.mymodule.say_hello()

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 ( . ).

We can also assign a shorter name for the module using an import — as statement:

import subdir.mymodule as m m.say_hello()

where m is any name we choose. We can also import the function directly:

from subdir.mymodule import say_hello say_hello()

This works even if there are multiple levels of subdirectories. For example, if we had the following directory structure:

we could write in script.py :

import alpha.beta.mymodule as mymodule mymodule.say_hello()

Import a File in a Different Directory

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:

import sys for p in sys.path: print( p )

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 :

import sys sys.path.append( '/alpha/beta' ) import mymodule mymodule.say_hello()

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:

import os import sys script_dir = os.path.dirname( __file__ ) mymodule_dir = os.path.join( script_dir, '..', 'alpha', 'beta' ) sys.path.append( mymodule_dir ) import mymodule mymodule.say_hello()

Import Any File, Including Non- .py File Extension (Python 3.4 and Up)

Absolute Path

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).

For example, let’s say we have the following directory structure:

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 :

import importlib.machinery import importlib.util # Import mymodule loader = importlib.machinery.SourceFileLoader( 'mymodule', '/alpha/beta/mymodule' ) spec = importlib.util.spec_from_loader( 'mymodule', loader ) mymodule = importlib.util.module_from_spec( spec ) loader.exec_module( mymodule ) # Use mymodule mymodule.say_hello()

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.

Relative 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:

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()

References

Источник

Оцените статью