Script python file

Script python file

I wanted to script a python file like a .m file in MATLAB and tried to find out

% run -i (for IPython environment)

Condition: IPython (Jupyter Notebook, etc.)

In the folder with "test.py"

%run -i test.py

Allows execution with the "test.py" script.

The namespace will be the same as the main one. It is also possible to put it in for.

exec command (method that can be used other than IPython) 2018.9.7 postscript

In the folder with "test.py"

with open('test.py', 'r', encoding='utf-8') as f:
    script = f.read()#Read the script file as a string
    exec(script) #Execute string with exec

Can be executed by the script of "test.py"

Normally, when import is used as a module from another file, global variables are different for each file, so it seems that it will not be a pure script. It should be used only for personal experiments, as the namespace spans different files and readability deteriorates.

Recommended Posts

Script python file
Execute Python script from batch file
python script skeleton
Python script profiling
Import python script
Python file processing
File operations in Python
[Python] File / directory operations
[Note] File reading ~ Python ~
File processing in Python
Automatically execute python file
Reading pyc file (Python 3.5.2)
File operations in Python
DynamoDB Script Memo (Python)
Read Python csv file
python external file reading
Template of python script to read the contents of the file
Summary of python file operations
Python
Download the file in Python
POST json with Python3 script
Draw netCDF file with python
Bitcoin price monitor python script
Run illustrator script from python
A memorandum to run a python script in a bat file
Python script to create a JSON file from a CSV file
Download csv file with python
Assigned scaffolding macro in Python script file to F12 key
A Python script that saves a clipboard (GTK) image to a file.
Extract the xz file with python
File / folder path manipulation in Python
Python script for ldapsearch base64 decode
Easy encryption of file contents (Python)
Tips on Python file input / output
[Python] Write to csv file with Python
Save the binary file in Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
Linebot creation & file sharing in Python
Output to csv file with Python
Create an Excel file with Python3
Get the script path in Python
Create a binary file in Python
[python] Copy script to generate copy log
Python CSV file reading and writing
Execute Python Script during CodeSys # RunTime
AWS Layer Creation Script for python
Extract the targz file using python
Notes for Python file input / output
Python memo ① Folder and file operations
Python executable file conversion module comparison 2
Python CGI file created on Windows
ORC, Parquet file operations in Python
[Automation with python! ] Part 2: File operation
Launch a Python script as a service
[Python] File operation using if statement
Write a batch script with Python3.5 ~
File upload to Azure Storage (Python)
[python] Script that (should) update pwsh
Create a shell script to run the python file multiple times
kafka python