Differences in sys.path depending on how Python is started (v3.8.2)

Overview

There are several ways to start a Python program, which will result in different values for the module search path sys.path.

References

View v3.8.2 version

--Python setup and usage, 1. Command line and environment. Https://docs.python.org/ja/3/using/cmdline.html

Details

Put simply

--If you specify a file etc. directly, the directory related to it comes to the beginning of sys.path --If you specify a module with -m, the current directory becomes the first element. --In either case, the module name is the string " __main__ ".

The summary is as follows. The author didn't know that he could launch directories (packages) and .zip files.

pythonArguments after What is done Its module name(__name__) sys.pathFirst element of
<.py file> That file __main__ <The directory where the file is located>
<directory> <directory>/__main__.py __main__ <directory>
<.zip file> .zip fileInside__main__.py __main__ <.zip file>
-m <module> <module>.py __main__ Current directory(Empty string)
-m <package> <package>/__main__.py __main__ Current directory
-c <command> __main__ Current directory
- Script passed as standard input __main__ Current directory

Precautions when using -I

If you specify -I in the boot options, you have specified -E -s and the interpreter boots in ** isolated mode **. At this time, the above ** first element is not added to sys.path (due to the influence of -s). ** ** So basically, you can (absolutely) import only modules installed with pip install. Note that even the module next to the execution script cannot be imported [^ 1].

[^ 1]: The module next to the execution script cannot be imported relative to it. This is because relative import from the __main__ module is not possible.

Recommended Posts

Differences in sys.path depending on how Python is started (v3.8.2)
In Python, change the behavior of the method depending on how it is called
How is the progress? Let's get on with the boom ?? in Python
How to use is and == in Python
How to test that Exception is raised in python unittest
Test.py is not reflected on the web server in Python3.
How Python __dict__ is used
How to develop in Python
How to install OpenCV on Cloud9 and run it in Python
How to judge that the cross key is input in Python3
How to use python put in pyenv on macOS with PyCall
How to use the asterisk (*) in Python. Maybe this is all? ..
Difference in execution speed depending on how to write Cython function
I did a lot of research on how Python is executed
Getting Started with Python responder v2
Difference between == and is in python
Use fabric as is in python (fabric3)
How to collect images in Python
Python is UnicodeEncodeError in CodeBox docker
How to use SQLite in Python
How to get started with Python
How to use Mysql in python
There is no switch in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Python in is also an operator
Getting started with Python 3.8 on Windows
How to handle Japanese in Python
Get started with Python in Blender
Differences in authenticity between Python and JavaScript
[Introduction to Python] How to use class in Python?
Differences between Ruby and Python in scope
How to access environment variables in Python
"<" In python> pack ("<L", ...) is little endian
How to read pydoc on python interpreter
How to dynamically define variables in Python
How to do R chartr () in Python
Find files like find on linux in Python
[Python] [pandas] How is pd.DataFrame (). T implemented?
[Kivy] How to install Kivy on Windows [Python]
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
2017-03-17 CentOS 6.8> Python 3.6.0 introduced / started / stopped in virutalenv
[Itertools.permutations] How to put permutations in Python
Differences in syntax between Python and Java
How to work with BigQuery in Python
Run AzureKinect in Python on Christmas Eve.
How to get a stacktrace in python
How to erase Python 2.x on Mac.
How to display multiplication table in python
How to extract polygon area in Python
What is "mahjong" in the Python library? ??
Memorandum on how to use gremlin python
How to check opencv version in python
Hash in Perl is a dictionary in Python
Get Started with TopCoder in Python (2020 Edition)
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python