I want to get / execute variables / functions / classes of external files from Python

About this page

As the title says, it will be updated at any time

Main story

Get variables in an external file

Import an external file (.py is not required)      ↓ Get variables in the form of "external file.variables in external file"

(Example)

variable.py


number = 15
array = [3,4,5]
array_2 = [[1,2],[3,4],[5,6]] 

main.py


import variable
print(variable.number)
print(variable.array)
print(variable.array_2)

result


15
[3,4,5]
[[1,2],[3,4],[5,6]]

I want to get a function in an external file

External file functions can be used in the form of "from external file import function you want to use"

(Example)

scalarMultiplication.py


#A function that just multiplies a by b
def multi(a,b):
    return a*b

main.py


from scalarMultiplication import multi
multi(2,3)
#6 Execution result

I want to get the class of an external file

Available in the form of "from external file import class you want to use"

(Example)

Recommended Posts

I want to get / execute variables / functions / classes of external files from Python
I want to start a lot of processes from python
I want to use jar from python
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to get League of Legends data ②
I want to use ceres solver from python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to get League of Legends data ①
I want to detect images of cats from Instagram
[Python] I want to merge Excel files anyway (pandas.merge)
[Python3] I want to generate harassment names from Japanese!
[Python] I tried to get Json of squid ring 2
I want to get custom data attributes of html as elements using Python Selenium
I want to specify another version of Python with pyvenv
[Python] I want to get a common set between numpy
I want to get the operation information of yahoo route
I want to automatically attend online classes with Python + Selenium!
I want to send a message from Python to LINE Bot
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
I want to know the features of Python and pip
Keras I want to get the output of any layer !!
I want to get rid of import warnings from Pyright and pylint in VS Code
I want to debug with Python
I want to output the beginning of the next month with Python
I want to use an external library with IBM Cloud Functions
I want to use both key and value of Python iterator
Python techniques for those who want to get rid of beginners
I want to get information from fstab at the ssh connection destination and execute a command
I want to extract an arbitrary URL from the character string of the html source with python
I want to operate DB using Django's ORM from an external application
I want to build a Python environment
(Python Selenium) I want to check the settings of the download destination of WebDriver
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I tried to get the authentication code of Qiita API with Python.
I want to play with aws with python
[python] Get a list of instance variables
Modules of frequently used functions in Python (such as reading external files)
I want to color a part of an Excel string in Python
[Python] I was addicted to not saving internal variables of lambda expressions
I tried to get the movie information of TMDb API with Python
Python: I want to measure the processing time of a function neatly
[Lambda] I tried to incorporate an external module of python via S3
I want to pass an argument to a python function and execute it from PHP on a web server
I want to get the path of the directory where the running file is stored.
[Python] I want to know the variables in the function when an error occurs!
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to send Gmail with Python, but I can't because of an error
I want to see a list of WebDAV files in the Requests module
I want to crop the image along the contour instead of the rectangle [python OpenCV]
About the handling of ZIP files including Japanese files when upgrading from Python2 to Python3
[Python] I tried to get the type name as a string from the type function
I want to get the file name, line number, and function name in Python 3.4
I tried to execute Python code from .Net using Pythonnet (Hallo World edition)
How to get a list of files in the same directory with python
[Python] I want to collect specific files scattered in each folder in one place
Get the contents of git diff from python
I want to do Dunnett's test in Python