본문 바로가기

OS/Linux

[python] Python Scripts

1. Shell Variables

Unlike Python, the method of defining a variable through the command language sets the variable name only in uppercase letters. Because the shell destinguishes the command from the parameter options based on the blanks, it is marked with "" if there is a blank in the value of the variable. In addition, the assignment operator must be defined in the variable name without spaces, which is the same as the aforementioned reason. 

 

user@host: /home$ VARIABLE_NAME = "contenst with blank"

 

If we want to load value of a variable, use the echo command and the access operator $ to output the value of the variable.

 

user@host: /home$ echo $VARIABLE_NAME

 

An environment variable is a variable that can be called by any program running in the shell. It is defined the export command, and in the case of Python interpreters, it can be called through the environ method of the os library.

 

user@host: /home$ export VARIABLE_NAME="contents with blank" 

# Call environment variable on python interpreter
user@host: /home$ python
user@host: /home$ import os 
user@host: /home$ print(os.environ["VARIABLE_NAME"])

 

The PATH environment variable omits the path name specified within the PATH variable for the execution program and allows the program to be executed. For example, wehn we run Python interface, we can run it through /usr/bin/python, but we can enter python to run it.

 

In other words, the PATH envrionment variable is a variable that allows the program to be executed even if the entire path is not known by storing the directory of the executable program located in location.

 

# Set PATH environment variable
export PATH="/usr/bin:$PATH"

 

2. Python Scripts

Python script is a program that executes and then stops soft sentences in a file using Python language. Altough Python code can be executed throug python commands inside the shell, typing one line at a time when trying to run a program of some size is not efficient. Therefore, it is efficient to enter code for each task inside the text file and execute it all at once.

 

In general, there is a method of developing through Python IDE such as Jupyter, Pycharm, and VSCode, or running on the shell through text editor. Redirection and nano commands are used to generate text files inside the shell.

 

2.1 __main__

If __name__ == "__main__": syntax often exists in Python script's files. This means that the code inside the code block should be executed only if the Python script is not imported into another script, but is executed directly from the interpreter.

 

2.2 Virtual Environment

Virtual Environment provides an independent Python execution environment for each project on a single PC and allows separate packages and versions to be used. This prevents unnecessary issues caused by system-wide package installation because different versions and packages can be installed for each project to be separated from other projects.

 

# Install virtual environment 
user@host: /home$ virtualenv -p /usr/bin/python3 virenv3

# Execute virtual environment 
user@host: /home$ source python3/bin/activate virenv3

# Check version of python 
user@host: /home$ python -V 

# Check installed packages in virtual environment 
user@host: /home$ pip freeze

# Deactivate virtual environment 
user@host: /home$ deacrivate

 

2.3 Import function to another script

Functions generated within one script can be called to another Python script. Just like importing a Python package and using a method, we can import the script and use the internal function like a method.

 

import script1

if __name__ == "__main__" :
    script1.function1()

 

2.4 Pass option into Python scripts

There is a way to pass the arguments entered from the command line inside the Python script, just as we apply the option to the command. Python's sys library stores the values entered as positional arguments in the list of sys.argv.

 

user@host: /home$ python script.py "argument1"