Мы уже знаем, что на текущую сессию интерпретатора изменение PYTHONPATH никак не повлияет. Но если вы запустите дочерний процесс, то он унаследует окружение текущего процесса, а значит и изменения в любых переменных будут на него влиять.
Вот небольшой пример:
Объявляем переменную
user@host:~$ export PYTHONPATH=/path1
Запускаем интерпретатор
user@host:~$ python3
Проверим что в sys.path
>>> import sys
>>> print(sys.path)
['', '/path1', '/usr/lib/...', ...]
Добавляем что-то в переменную
>>> import os
>>> os.emviron['PYTHONPATH'] = '/path1:/path2'
>>> print(sys.path)
['', '/path1', '/usr/lib/...', ...]
Изменений нет. Но давайте запустим дочерний процесс и посмотрим там
>>> os.system('python3')
# теперь мы находимся в другом процессе
>>> import sys
>>> print(sys.path)
['', '/path1', '/path2', '/usr/lib/...', ...]
Тоже самое будет и с subprocess, так как по умолчанию текущее окружение тоже наследуется.
>>> import subprocess
>>> subprocess.call(['python3', '-c', 'import sys;print(sys.path)'])
['', '/path1', '/path2', '/usr/lib/...', ...]
______________________
Лучшей практикой является передача энвайронмента явно через аргумент env!
import subprocess
subprocess.call(cmd, env={'PYTHONPATH': '...'})
Это поможет точно понимать какое окружение будет у запускаемого процесса и при этом не изменять окружение текущего процесса.
#basic
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called "Winter x Soobin" from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa💄. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called " Minji Moodboard " from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa💄. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called " Yunjin Moodboard " from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa💄. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called " Go Younjung MoodBoard" from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa💄. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called " Eunchae Moodstagram " from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa💄. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called " Lia MoodBoard " from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa💄. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called " Hanni Moodstagram " from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa l. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀💄: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #SpaceShip called "Yunjin MoodBoard " from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #Awäa💄. Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀🌧: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #Spaceship called "Rose" from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #revaya . Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀🌧: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #Spaceship called "Hueningkai" from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #revaya . Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)
👩🏻🚀🌧: *𝗕𝗢𝗢𝗠* 🤯🤯 Woah 😵💫😵... What was that 🤔🤔? Its was a super-duper big *boom* 💥💥 that just come to our 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹 𝗺𝗼𝗼𝗻 🌑🌕, and look. They giving us a super-duper adorable 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 📦📦, lets check it now! Ooh 😯😯, its a #Spaceship called "Kyujin" from our 𝗳𝗿𝗶𝗲𝗻𝗱𝘀 👨👩👧👦, which is #revaya . Ah, thank you @moonivrse lets check the licence first! Oh, its only for PU (Personal Use)