Skip to main content
add about X.X version, show the search as a readable list, minor readability fixes
Source Link
moopet
  • 6.2k
  • 1
  • 31
  • 38

I will describe the basic process, which I learned from the presentation which jcollado@jcollado linked to.

When Python starts, it looks at the path of the binary, and the prefixes thereof.

So let's say your virtualenv is /home/blah/scratch. The Python process knows it was executed from /home/blah/scratch/bin/python (which is usually just a copy of your system python binary /usr/bin/python) and it knows its own version X.Y because it's compiled into it. Then Python looks for lib/pythonX.Y/os.py in this order:

/home/blah/scratch/bin/lib/pythonX.Y/os.py
/home/blah/scratch/lib/pythonX.Y/os.py    <-- this file should exist
/home/blah/lib/pythonX.Y/os.py
/home/lib/pythonX.Y/os.py
/lib/pythonX.Y/os.py

It stops at /home/blah/scratch/lib/pythonX.Y/os.py because it's the first file that actually exists. If it didn't, Python would keep looking. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.

I will describe the basic process, which I learned from the presentation which jcollado linked to.

When Python starts, it looks at the path of binary, and the prefixes thereof.

So let's say your virtualenv is /home/blah/scratch. Python process knows it was executed from /home/blah/scratch/bin/python (which is usually just a copy of your system python binary /usr/bin/python) and it knows its own version X.Y because it's compiled into it. Then Python looks for lib/pythonX.Y/os.py in this order:

/home/blah/scratch/bin/lib/pythonX.Y/os.py
/home/blah/scratch/lib/pythonX.Y/os.py    <-- this file should exist
/home/blah/lib/pythonX.Y/os.py
/home/lib/pythonX.Y/os.py
/lib/pythonX.Y/os.py

It stops at /home/blah/scratch/lib/pythonX.Y/os.py because it's the first file that actually exists. If it didn't, Python would keep looking. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.

I will describe the basic process, which I learned from the presentation @jcollado linked to.

When Python starts, it looks at the path of the binary, and the prefixes thereof.

So let's say your virtualenv is /home/blah/scratch. The Python process knows it was executed from /home/blah/scratch/bin/python (which is usually just a copy of your system python binary /usr/bin/python) and it knows its own version X.Y because it's compiled into it. Then Python looks for lib/pythonX.Y/os.py in this order:

/home/blah/scratch/bin/lib/pythonX.Y/os.py
/home/blah/scratch/lib/pythonX.Y/os.py    <-- this file should exist
/home/blah/lib/pythonX.Y/os.py
/home/lib/pythonX.Y/os.py
/lib/pythonX.Y/os.py

It stops at /home/blah/scratch/lib/pythonX.Y/os.py because it's the first file that actually exists. If it didn't, Python would keep looking. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.

add about X.X version, show the search as a readable list, minor readability fixes
Source Link

I will describe the basic process, which I learned from the presentation which jcollado linked to.

When Python starts, it looks at the path of binary, and the prefixes thereof. 

So let's say your virtualenv is /home/blah/scratch. Then the python binaryPython process knows it was executed from /home/blah/scratch/bin/python (which is usually just a copy of your system python binary) will be in /home/blah/scratchusr/bin/python) and it knows its own version X.Y because it's compiled into it. Then Python looks for lib/pythonX.XY/os.py in /home/blah/scratch/bin/, then /home/blah/scratch/, and itthis order:

/home/blah/scratch/bin/lib/pythonX.Y/os.py
/home/blah/scratch/lib/pythonX.Y/os.py    <-- this file should exist
/home/blah/lib/pythonX.Y/os.py
/home/lib/pythonX.Y/os.py
/lib/pythonX.Y/os.py

It stops there becauseat /home/blah/scratch/lib/pythonX.XY/os.py because it's the first file that actually exists. If it didn't, Python would keep looking for /home/blah/lib/pythonX.X/os.py, etc. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.

I will describe the basic process, which I learned from the presentation which jcollado linked to.

When Python starts, it looks at the path of binary, and the prefixes thereof. So let's say your virtualenv is /home/blah/scratch. Then the python binary (which is just a copy of your system python binary) will be in /home/blah/scratch/bin/python. Python looks for lib/pythonX.X/os.py in /home/blah/scratch/bin/, then /home/blah/scratch/, and it stops there because /home/blah/scratch/lib/pythonX.X/os.py exists. If it didn't, Python would keep looking for /home/blah/lib/pythonX.X/os.py, etc. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.

I will describe the basic process, which I learned from the presentation which jcollado linked to.

When Python starts, it looks at the path of binary, and the prefixes thereof. 

So let's say your virtualenv is /home/blah/scratch. Python process knows it was executed from /home/blah/scratch/bin/python (which is usually just a copy of your system python binary /usr/bin/python) and it knows its own version X.Y because it's compiled into it. Then Python looks for lib/pythonX.Y/os.py in this order:

/home/blah/scratch/bin/lib/pythonX.Y/os.py
/home/blah/scratch/lib/pythonX.Y/os.py    <-- this file should exist
/home/blah/lib/pythonX.Y/os.py
/home/lib/pythonX.Y/os.py
/lib/pythonX.Y/os.py

It stops at /home/blah/scratch/lib/pythonX.Y/os.py because it's the first file that actually exists. If it didn't, Python would keep looking. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.

Source Link
Max
  • 1.7k
  • 16
  • 22

I will describe the basic process, which I learned from the presentation which jcollado linked to.

When Python starts, it looks at the path of binary, and the prefixes thereof. So let's say your virtualenv is /home/blah/scratch. Then the python binary (which is just a copy of your system python binary) will be in /home/blah/scratch/bin/python. Python looks for lib/pythonX.X/os.py in /home/blah/scratch/bin/, then /home/blah/scratch/, and it stops there because /home/blah/scratch/lib/pythonX.X/os.py exists. If it didn't, Python would keep looking for /home/blah/lib/pythonX.X/os.py, etc. It then sets sys.prefix based on this. It uses a similar process to set sys.exec_prefix, and then sys.path is constructed based on these.