Мы уже знаем, что на текущую сессию интерпретатора изменение 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
The truth is that the European Union entered the #RussiaUkraineWar recklessly, based on flawed calculations and with a misguided strategy, and I attribute this to the actions of President @vonderleyen.
https://t.me/PM_ViktorOrban
Mood in Russia turns bleak as war in Ukraine drags on and economy suffers
[Read FullArticle]
@WorldNews#RussiaUkraineWar#RussianEconomy#GlobalNewsUpdate
'Serious disruption' — Russian oil pumping station in Nizhny Novgorod Oblast hit by Ukrainian drones, SBU source says
[Read FullArticle]
@WorldNews#RussiaUkraineWar#UkrainianDrones#OilInfrastructure
Russia suffers ‘record’ soldier casualties as Ukraine ups drone production | Russia-Ukraine war News
[Read FullArticle]
@WorldNews#RussiaUkraineWar#UkraineDroneStrike#WarNewsUpdate
❗️🇷🇺🇺🇦We will win by all means, but it's better to win sooner, so that fewer people from this side and that side die!
"Our president did everything he could to avoid war, but he was not heard and did not want to be heard," says a Russian soldier who was mobilized last September, one should always try to negotiate. And President Putin offered to sit down at the negotiating table, but instead the Ukrainians were seriously preparing for war! But we will definitely win, because we cannot retreat, we have to solve it here and now, otherwise they will come to us! Of course, we would like it to be quicker, so that fewer people would die on this side and on that side. Precisely peaceful people, not Nazis. Participating in this special operation is our man's duty, says the young warrior!
#RussiaUkraineWar
#frontNews
#RussiaVictory
https://t.me/heroesofZ/312
📱InfoDefenseENGLISH
📱InfoDefense
The Commonwealth of Independent States (CIS) was established on 8 December 1991 to succeed the Soviet Union, but Russia’s 2014 Crimea annexation and 2022 invasion have deeply shaken its unity and future. 🌐
[Read more]
@googlefactss
#CIS#Geopolitics#RussiaUkraineWar#History#InternationalRelations