Consequences for virtual env when system's Python is removed and/or updated2019 Community Moderator ElectionHow can I remove a trailing newline in Python?How do I remove an element from a list by index in Python?Why do people write the #!/usr/bin/env python shebang on the first line of a Python script?How to remove a key from a Python dictionary?How to upgrade virtual environment from previous Python version?virtual env python 3.5 only finds django python 2.7Python version in virtualenv - ubuntu 16.04UnicodeDecodeError during plot.show() in python virtual environmentHow to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?How to execute virtualenv from /Library/Frameworks/Python.framework/Versions/.. in Mac
Are small insurances worth it?
Having the player face themselves after the mid-game
Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?
Unfamiliar notation in Diabelli's "Duet in D" for piano
What the error in writing this equation by latex?
How to recover against Snake as a heavyweight character?
Book where society has been split into 2 with a wall down the middle where one side embraced high tech whereas other side were totally against tech
Draw this image in the TIKZ package
Create chunks from an array
Why would /etc/passwd be used every time someone executes `ls -l` command?
The (Easy) Road to Code
Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?
If nine coins are tossed, what is the probability that the number of heads is even?
What is Tony Stark injecting into himself in Iron Man 3?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
Unidentified signals on FT8 frequencies
Has a sovereign Communist government ever run, and conceded loss, on a fair election?
Why does this boat have a landing pad? (SpaceX's GO Searcher) Any plans for propulsive capsule landings?
What is better: yes / no radio, or simple checkbox?
Why restrict private health insurance?
Tabular environment - text vertically positions itself by bottom of tikz picture in adjacent cell
How would an energy-based "projectile" blow up a spaceship?
Rationale to prefer local variables over instance variables?
Does the US political system, in principle, allow for a no-party system?
Consequences for virtual env when system's Python is removed and/or updated
2019 Community Moderator ElectionHow can I remove a trailing newline in Python?How do I remove an element from a list by index in Python?Why do people write the #!/usr/bin/env python shebang on the first line of a Python script?How to remove a key from a Python dictionary?How to upgrade virtual environment from previous Python version?virtual env python 3.5 only finds django python 2.7Python version in virtualenv - ubuntu 16.04UnicodeDecodeError during plot.show() in python virtual environmentHow to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?How to execute virtualenv from /Library/Frameworks/Python.framework/Versions/.. in Mac
EDIT FOR CLARITY: This question is in reference to venv, which is often confused with virtualenv.
What are the consequences of removing the system's Python installation which was the source used to create virtual environments on the system using venv
?
Ultimately, what I'm looking to do is remove the 3.7 installation and install 3.8 when it is released, but I'm unsure of how strongly tethered a virtual environment is to the system environment.
Some system details that may be relevant when asking this question:
- OS = Red Hat Enterprise Linux 7
- 3.7 installed from source at Python.org, not the
rh-Pythonx.x
packge in the yum repo. - Python 3.7 is installed to the default location,
/usr/local/bin/python3.7
/usr/local/bin/python3
is a link to/usr/local/bin/python3.7
Given the above details, I am under the impression that Python 3.8 would install to /usr/local/bin/python3.8
and python3
would then be pointing to that version.
Does the virtual environment have enough within it to be a stable 3.7 environment of its own? Or will it fall apart, trying to reference a system install that no longer exists?
tl;dr:
What happens to all those virtual environments that were created from a Python source that is no longer existent / replaced with a newer version?
python upgrade python-venv virtual-environment
add a comment |
EDIT FOR CLARITY: This question is in reference to venv, which is often confused with virtualenv.
What are the consequences of removing the system's Python installation which was the source used to create virtual environments on the system using venv
?
Ultimately, what I'm looking to do is remove the 3.7 installation and install 3.8 when it is released, but I'm unsure of how strongly tethered a virtual environment is to the system environment.
Some system details that may be relevant when asking this question:
- OS = Red Hat Enterprise Linux 7
- 3.7 installed from source at Python.org, not the
rh-Pythonx.x
packge in the yum repo. - Python 3.7 is installed to the default location,
/usr/local/bin/python3.7
/usr/local/bin/python3
is a link to/usr/local/bin/python3.7
Given the above details, I am under the impression that Python 3.8 would install to /usr/local/bin/python3.8
and python3
would then be pointing to that version.
Does the virtual environment have enough within it to be a stable 3.7 environment of its own? Or will it fall apart, trying to reference a system install that no longer exists?
tl;dr:
What happens to all those virtual environments that were created from a Python source that is no longer existent / replaced with a newer version?
python upgrade python-venv virtual-environment
1
Note that you can safely install multiple versions of Python side by side. There is no technical reason to remove your Python 3.7 installation before installing 3.8.
– Code-Apprentice
2 days ago
@Code-Apprentice: Well, depends on how you install it. I'd strongly recommend usingmake altinstall
when installing 3.8 from source, so the system defaultpython3
remains whatever shipped with the OS and any scripts shipped with the OS that havepython3
dependencies don't suddenly have Python upgraded under them in a potentially breaking way (it's rare, but there are scripts/modules that depend on internals of a given minor release and break in subsequent releases).
– ShadowRanger
2 days ago
add a comment |
EDIT FOR CLARITY: This question is in reference to venv, which is often confused with virtualenv.
What are the consequences of removing the system's Python installation which was the source used to create virtual environments on the system using venv
?
Ultimately, what I'm looking to do is remove the 3.7 installation and install 3.8 when it is released, but I'm unsure of how strongly tethered a virtual environment is to the system environment.
Some system details that may be relevant when asking this question:
- OS = Red Hat Enterprise Linux 7
- 3.7 installed from source at Python.org, not the
rh-Pythonx.x
packge in the yum repo. - Python 3.7 is installed to the default location,
/usr/local/bin/python3.7
/usr/local/bin/python3
is a link to/usr/local/bin/python3.7
Given the above details, I am under the impression that Python 3.8 would install to /usr/local/bin/python3.8
and python3
would then be pointing to that version.
Does the virtual environment have enough within it to be a stable 3.7 environment of its own? Or will it fall apart, trying to reference a system install that no longer exists?
tl;dr:
What happens to all those virtual environments that were created from a Python source that is no longer existent / replaced with a newer version?
python upgrade python-venv virtual-environment
EDIT FOR CLARITY: This question is in reference to venv, which is often confused with virtualenv.
What are the consequences of removing the system's Python installation which was the source used to create virtual environments on the system using venv
?
Ultimately, what I'm looking to do is remove the 3.7 installation and install 3.8 when it is released, but I'm unsure of how strongly tethered a virtual environment is to the system environment.
Some system details that may be relevant when asking this question:
- OS = Red Hat Enterprise Linux 7
- 3.7 installed from source at Python.org, not the
rh-Pythonx.x
packge in the yum repo. - Python 3.7 is installed to the default location,
/usr/local/bin/python3.7
/usr/local/bin/python3
is a link to/usr/local/bin/python3.7
Given the above details, I am under the impression that Python 3.8 would install to /usr/local/bin/python3.8
and python3
would then be pointing to that version.
Does the virtual environment have enough within it to be a stable 3.7 environment of its own? Or will it fall apart, trying to reference a system install that no longer exists?
tl;dr:
What happens to all those virtual environments that were created from a Python source that is no longer existent / replaced with a newer version?
python upgrade python-venv virtual-environment
python upgrade python-venv virtual-environment
edited 2 days ago
Joshua Schlichting
asked 2 days ago
Joshua SchlichtingJoshua Schlichting
1,0731124
1,0731124
1
Note that you can safely install multiple versions of Python side by side. There is no technical reason to remove your Python 3.7 installation before installing 3.8.
– Code-Apprentice
2 days ago
@Code-Apprentice: Well, depends on how you install it. I'd strongly recommend usingmake altinstall
when installing 3.8 from source, so the system defaultpython3
remains whatever shipped with the OS and any scripts shipped with the OS that havepython3
dependencies don't suddenly have Python upgraded under them in a potentially breaking way (it's rare, but there are scripts/modules that depend on internals of a given minor release and break in subsequent releases).
– ShadowRanger
2 days ago
add a comment |
1
Note that you can safely install multiple versions of Python side by side. There is no technical reason to remove your Python 3.7 installation before installing 3.8.
– Code-Apprentice
2 days ago
@Code-Apprentice: Well, depends on how you install it. I'd strongly recommend usingmake altinstall
when installing 3.8 from source, so the system defaultpython3
remains whatever shipped with the OS and any scripts shipped with the OS that havepython3
dependencies don't suddenly have Python upgraded under them in a potentially breaking way (it's rare, but there are scripts/modules that depend on internals of a given minor release and break in subsequent releases).
– ShadowRanger
2 days ago
1
1
Note that you can safely install multiple versions of Python side by side. There is no technical reason to remove your Python 3.7 installation before installing 3.8.
– Code-Apprentice
2 days ago
Note that you can safely install multiple versions of Python side by side. There is no technical reason to remove your Python 3.7 installation before installing 3.8.
– Code-Apprentice
2 days ago
@Code-Apprentice: Well, depends on how you install it. I'd strongly recommend using
make altinstall
when installing 3.8 from source, so the system default python3
remains whatever shipped with the OS and any scripts shipped with the OS that have python3
dependencies don't suddenly have Python upgraded under them in a potentially breaking way (it's rare, but there are scripts/modules that depend on internals of a given minor release and break in subsequent releases).– ShadowRanger
2 days ago
@Code-Apprentice: Well, depends on how you install it. I'd strongly recommend using
make altinstall
when installing 3.8 from source, so the system default python3
remains whatever shipped with the OS and any scripts shipped with the OS that have python3
dependencies don't suddenly have Python upgraded under them in a potentially breaking way (it's rare, but there are scripts/modules that depend on internals of a given minor release and break in subsequent releases).– ShadowRanger
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
Bad news: You're using venv
, and venv
virtual environments are not completely standalone. Even when created with --copies
, all it copies is the python
executable itself, not the standard libraries, not even (if your install creates a shared libpython
rather than a static one) libpython
; it relies on the system copy. The virtual environment will break if the Python install it was based on goes away. How it breaks will differ based on how it was created. For example, if you created it with:
python3 -mvenv path/to/venv
when python3
meant Python 3.7, then replaced python3
with Python 3.8, then you can fix up the virtual env for the new version with:
python3 -mvenv --upgrade path/to/venv
but your installed third party packages will be (effectively) gone (they'll be in path/to/venv/lib/python3.7
, but Python 3.8 will only be looking in path/to/venv/lib/python3.8
), so you'll have to reinstall them.
If you created the virtual environment with:
python3.7 -mvenv path/to/venv
then it's completely broken (at least as documented), the --upgrade
switch is only documented to work for upgrading when Python was upgraded in place; since the new Python would not be named python3.7
, you can't upgrade in place. That said, --upgrade
really only works well when upgrading micro versions (from 3.7.1 to 3.7.2 or the like), due to aforementioned per-minor version lib/pythonX.Y
directories, so either way, you're best off just creating a new virtual environment from scratch.
To be clear, the third party virtualenv
package does not have this limitation if and only if the system Python installation statically linked libpython
. Oddly, while the --always-copy
flag will make it copy the main binary and the standard library modules, it doesn't cause libpython
itself (the interpreter core) to be copied, so if the main binary depends on a system copy of libpython.so
, then removing the system copy breaks the virtual environment. If you did use --always-copy
and your python
executable statically linked libpython.a
(ldd /path/to/python3
should show no libpython
dependency), then yes, virtualenv
makes much more heavyweight (in local tests on 3.6, forcing copies via the appropriate switch, freshly created venv
environments were ~11 MB, while virtualenv
environments were ~48 MB; sadly, my python
dynamically links libpython.so
, so it still wouldn't work) virtual environments that should survive the removal of the system installed copy of Python.
In any event, it's probably best to leave your Python 3.7 install in place, and just upgrade to 3.8 without removing 3.7 (are you really hard up for a few dozen MB of disk space?). Even if you replace python3
with the new 3.8 install, python3.7
, libpython3.7m.so.1.0
or the like and the rest of the 3.7 standard library will continue to exist for the virtual environment to depend on; at worst you might need to manually change the symlink in the virtual environment to point to /path/to/python3.7
rather than /path/to/python3
to keep using the old version (including all your installed third party packages).
An alternative to trying to keep the old virtual environment working is to just back up the installed state of that virtual environment, delete it, install the new Python, create a fresh virtual environment, and then use the backed up state to reinstall all your packages in the upgraded virtual env. An example might be:
$ source ~/path/to/venv/bin/activate
$ pip freeze > installed_libs.txt
$ deactivate
$ rm -rf ~/path/to/venv
$ ... install new Python/remove old Python ...
$ python3 -mvenv ~/path/to/venv
$ pip install -r installed_libs.txt # Optionally add --upgrade to install latest, not fixed versions
add a comment |
A virtualenv includes a copy of the python binary itself, so nothing should happen. See a more detailed explanation here
virtualenv solves this problem by creating a completely isolated virtual environment for each of your programs. An environment is simply a directory that contains a complete copy of everything needed to run a Python program, including a copy of the python binary itself, a copy of the entire Python standard library, a copy of the pip installer, and (crucially) a copy of the site-packages directory mentioned above. When you install a package from PyPI using the copy of pip that's created by the virtualenv tool, it will install the package into the site-packages directory inside the virtualenv directory. You can then use it in your program just as before.
Edit (accounting for venv vs virtualenv): According to the venv-documentation, it can either copy or symlink the binary:
The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
3
It should be noted for anyone reading this that whilevenv
andvirtualenv
are similar, they are not the same package.
– Joshua Schlichting
2 days ago
Good point, edited the answer accordingly
– dorvak
2 days ago
This answer says a completely different thing than the other answers to this question, namely that it does not break when updating the system python, because it can copy it
– Ferrybig
2 days ago
@Ferrybig: That's becausevirtualenv
(third party package) andvenv
(built-in part of Python standard library) behave differently. This answer was originally answered from the perspective ofvirtualenv
(they missed the OP saying they usedvenv
). As it happens,virtualenv
still isn't guaranteed to work if the system Python goes away sincelibpython.so
isn't copied (which seems like a bug invirtualenv
, given its guarantees about standalone behavior), but it has a chance to work, wherevenv
definitely won't.
– ShadowRanger
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55025873%2fconsequences-for-virtual-env-when-systems-python-is-removed-and-or-updated%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Bad news: You're using venv
, and venv
virtual environments are not completely standalone. Even when created with --copies
, all it copies is the python
executable itself, not the standard libraries, not even (if your install creates a shared libpython
rather than a static one) libpython
; it relies on the system copy. The virtual environment will break if the Python install it was based on goes away. How it breaks will differ based on how it was created. For example, if you created it with:
python3 -mvenv path/to/venv
when python3
meant Python 3.7, then replaced python3
with Python 3.8, then you can fix up the virtual env for the new version with:
python3 -mvenv --upgrade path/to/venv
but your installed third party packages will be (effectively) gone (they'll be in path/to/venv/lib/python3.7
, but Python 3.8 will only be looking in path/to/venv/lib/python3.8
), so you'll have to reinstall them.
If you created the virtual environment with:
python3.7 -mvenv path/to/venv
then it's completely broken (at least as documented), the --upgrade
switch is only documented to work for upgrading when Python was upgraded in place; since the new Python would not be named python3.7
, you can't upgrade in place. That said, --upgrade
really only works well when upgrading micro versions (from 3.7.1 to 3.7.2 or the like), due to aforementioned per-minor version lib/pythonX.Y
directories, so either way, you're best off just creating a new virtual environment from scratch.
To be clear, the third party virtualenv
package does not have this limitation if and only if the system Python installation statically linked libpython
. Oddly, while the --always-copy
flag will make it copy the main binary and the standard library modules, it doesn't cause libpython
itself (the interpreter core) to be copied, so if the main binary depends on a system copy of libpython.so
, then removing the system copy breaks the virtual environment. If you did use --always-copy
and your python
executable statically linked libpython.a
(ldd /path/to/python3
should show no libpython
dependency), then yes, virtualenv
makes much more heavyweight (in local tests on 3.6, forcing copies via the appropriate switch, freshly created venv
environments were ~11 MB, while virtualenv
environments were ~48 MB; sadly, my python
dynamically links libpython.so
, so it still wouldn't work) virtual environments that should survive the removal of the system installed copy of Python.
In any event, it's probably best to leave your Python 3.7 install in place, and just upgrade to 3.8 without removing 3.7 (are you really hard up for a few dozen MB of disk space?). Even if you replace python3
with the new 3.8 install, python3.7
, libpython3.7m.so.1.0
or the like and the rest of the 3.7 standard library will continue to exist for the virtual environment to depend on; at worst you might need to manually change the symlink in the virtual environment to point to /path/to/python3.7
rather than /path/to/python3
to keep using the old version (including all your installed third party packages).
An alternative to trying to keep the old virtual environment working is to just back up the installed state of that virtual environment, delete it, install the new Python, create a fresh virtual environment, and then use the backed up state to reinstall all your packages in the upgraded virtual env. An example might be:
$ source ~/path/to/venv/bin/activate
$ pip freeze > installed_libs.txt
$ deactivate
$ rm -rf ~/path/to/venv
$ ... install new Python/remove old Python ...
$ python3 -mvenv ~/path/to/venv
$ pip install -r installed_libs.txt # Optionally add --upgrade to install latest, not fixed versions
add a comment |
Bad news: You're using venv
, and venv
virtual environments are not completely standalone. Even when created with --copies
, all it copies is the python
executable itself, not the standard libraries, not even (if your install creates a shared libpython
rather than a static one) libpython
; it relies on the system copy. The virtual environment will break if the Python install it was based on goes away. How it breaks will differ based on how it was created. For example, if you created it with:
python3 -mvenv path/to/venv
when python3
meant Python 3.7, then replaced python3
with Python 3.8, then you can fix up the virtual env for the new version with:
python3 -mvenv --upgrade path/to/venv
but your installed third party packages will be (effectively) gone (they'll be in path/to/venv/lib/python3.7
, but Python 3.8 will only be looking in path/to/venv/lib/python3.8
), so you'll have to reinstall them.
If you created the virtual environment with:
python3.7 -mvenv path/to/venv
then it's completely broken (at least as documented), the --upgrade
switch is only documented to work for upgrading when Python was upgraded in place; since the new Python would not be named python3.7
, you can't upgrade in place. That said, --upgrade
really only works well when upgrading micro versions (from 3.7.1 to 3.7.2 or the like), due to aforementioned per-minor version lib/pythonX.Y
directories, so either way, you're best off just creating a new virtual environment from scratch.
To be clear, the third party virtualenv
package does not have this limitation if and only if the system Python installation statically linked libpython
. Oddly, while the --always-copy
flag will make it copy the main binary and the standard library modules, it doesn't cause libpython
itself (the interpreter core) to be copied, so if the main binary depends on a system copy of libpython.so
, then removing the system copy breaks the virtual environment. If you did use --always-copy
and your python
executable statically linked libpython.a
(ldd /path/to/python3
should show no libpython
dependency), then yes, virtualenv
makes much more heavyweight (in local tests on 3.6, forcing copies via the appropriate switch, freshly created venv
environments were ~11 MB, while virtualenv
environments were ~48 MB; sadly, my python
dynamically links libpython.so
, so it still wouldn't work) virtual environments that should survive the removal of the system installed copy of Python.
In any event, it's probably best to leave your Python 3.7 install in place, and just upgrade to 3.8 without removing 3.7 (are you really hard up for a few dozen MB of disk space?). Even if you replace python3
with the new 3.8 install, python3.7
, libpython3.7m.so.1.0
or the like and the rest of the 3.7 standard library will continue to exist for the virtual environment to depend on; at worst you might need to manually change the symlink in the virtual environment to point to /path/to/python3.7
rather than /path/to/python3
to keep using the old version (including all your installed third party packages).
An alternative to trying to keep the old virtual environment working is to just back up the installed state of that virtual environment, delete it, install the new Python, create a fresh virtual environment, and then use the backed up state to reinstall all your packages in the upgraded virtual env. An example might be:
$ source ~/path/to/venv/bin/activate
$ pip freeze > installed_libs.txt
$ deactivate
$ rm -rf ~/path/to/venv
$ ... install new Python/remove old Python ...
$ python3 -mvenv ~/path/to/venv
$ pip install -r installed_libs.txt # Optionally add --upgrade to install latest, not fixed versions
add a comment |
Bad news: You're using venv
, and venv
virtual environments are not completely standalone. Even when created with --copies
, all it copies is the python
executable itself, not the standard libraries, not even (if your install creates a shared libpython
rather than a static one) libpython
; it relies on the system copy. The virtual environment will break if the Python install it was based on goes away. How it breaks will differ based on how it was created. For example, if you created it with:
python3 -mvenv path/to/venv
when python3
meant Python 3.7, then replaced python3
with Python 3.8, then you can fix up the virtual env for the new version with:
python3 -mvenv --upgrade path/to/venv
but your installed third party packages will be (effectively) gone (they'll be in path/to/venv/lib/python3.7
, but Python 3.8 will only be looking in path/to/venv/lib/python3.8
), so you'll have to reinstall them.
If you created the virtual environment with:
python3.7 -mvenv path/to/venv
then it's completely broken (at least as documented), the --upgrade
switch is only documented to work for upgrading when Python was upgraded in place; since the new Python would not be named python3.7
, you can't upgrade in place. That said, --upgrade
really only works well when upgrading micro versions (from 3.7.1 to 3.7.2 or the like), due to aforementioned per-minor version lib/pythonX.Y
directories, so either way, you're best off just creating a new virtual environment from scratch.
To be clear, the third party virtualenv
package does not have this limitation if and only if the system Python installation statically linked libpython
. Oddly, while the --always-copy
flag will make it copy the main binary and the standard library modules, it doesn't cause libpython
itself (the interpreter core) to be copied, so if the main binary depends on a system copy of libpython.so
, then removing the system copy breaks the virtual environment. If you did use --always-copy
and your python
executable statically linked libpython.a
(ldd /path/to/python3
should show no libpython
dependency), then yes, virtualenv
makes much more heavyweight (in local tests on 3.6, forcing copies via the appropriate switch, freshly created venv
environments were ~11 MB, while virtualenv
environments were ~48 MB; sadly, my python
dynamically links libpython.so
, so it still wouldn't work) virtual environments that should survive the removal of the system installed copy of Python.
In any event, it's probably best to leave your Python 3.7 install in place, and just upgrade to 3.8 without removing 3.7 (are you really hard up for a few dozen MB of disk space?). Even if you replace python3
with the new 3.8 install, python3.7
, libpython3.7m.so.1.0
or the like and the rest of the 3.7 standard library will continue to exist for the virtual environment to depend on; at worst you might need to manually change the symlink in the virtual environment to point to /path/to/python3.7
rather than /path/to/python3
to keep using the old version (including all your installed third party packages).
An alternative to trying to keep the old virtual environment working is to just back up the installed state of that virtual environment, delete it, install the new Python, create a fresh virtual environment, and then use the backed up state to reinstall all your packages in the upgraded virtual env. An example might be:
$ source ~/path/to/venv/bin/activate
$ pip freeze > installed_libs.txt
$ deactivate
$ rm -rf ~/path/to/venv
$ ... install new Python/remove old Python ...
$ python3 -mvenv ~/path/to/venv
$ pip install -r installed_libs.txt # Optionally add --upgrade to install latest, not fixed versions
Bad news: You're using venv
, and venv
virtual environments are not completely standalone. Even when created with --copies
, all it copies is the python
executable itself, not the standard libraries, not even (if your install creates a shared libpython
rather than a static one) libpython
; it relies on the system copy. The virtual environment will break if the Python install it was based on goes away. How it breaks will differ based on how it was created. For example, if you created it with:
python3 -mvenv path/to/venv
when python3
meant Python 3.7, then replaced python3
with Python 3.8, then you can fix up the virtual env for the new version with:
python3 -mvenv --upgrade path/to/venv
but your installed third party packages will be (effectively) gone (they'll be in path/to/venv/lib/python3.7
, but Python 3.8 will only be looking in path/to/venv/lib/python3.8
), so you'll have to reinstall them.
If you created the virtual environment with:
python3.7 -mvenv path/to/venv
then it's completely broken (at least as documented), the --upgrade
switch is only documented to work for upgrading when Python was upgraded in place; since the new Python would not be named python3.7
, you can't upgrade in place. That said, --upgrade
really only works well when upgrading micro versions (from 3.7.1 to 3.7.2 or the like), due to aforementioned per-minor version lib/pythonX.Y
directories, so either way, you're best off just creating a new virtual environment from scratch.
To be clear, the third party virtualenv
package does not have this limitation if and only if the system Python installation statically linked libpython
. Oddly, while the --always-copy
flag will make it copy the main binary and the standard library modules, it doesn't cause libpython
itself (the interpreter core) to be copied, so if the main binary depends on a system copy of libpython.so
, then removing the system copy breaks the virtual environment. If you did use --always-copy
and your python
executable statically linked libpython.a
(ldd /path/to/python3
should show no libpython
dependency), then yes, virtualenv
makes much more heavyweight (in local tests on 3.6, forcing copies via the appropriate switch, freshly created venv
environments were ~11 MB, while virtualenv
environments were ~48 MB; sadly, my python
dynamically links libpython.so
, so it still wouldn't work) virtual environments that should survive the removal of the system installed copy of Python.
In any event, it's probably best to leave your Python 3.7 install in place, and just upgrade to 3.8 without removing 3.7 (are you really hard up for a few dozen MB of disk space?). Even if you replace python3
with the new 3.8 install, python3.7
, libpython3.7m.so.1.0
or the like and the rest of the 3.7 standard library will continue to exist for the virtual environment to depend on; at worst you might need to manually change the symlink in the virtual environment to point to /path/to/python3.7
rather than /path/to/python3
to keep using the old version (including all your installed third party packages).
An alternative to trying to keep the old virtual environment working is to just back up the installed state of that virtual environment, delete it, install the new Python, create a fresh virtual environment, and then use the backed up state to reinstall all your packages in the upgraded virtual env. An example might be:
$ source ~/path/to/venv/bin/activate
$ pip freeze > installed_libs.txt
$ deactivate
$ rm -rf ~/path/to/venv
$ ... install new Python/remove old Python ...
$ python3 -mvenv ~/path/to/venv
$ pip install -r installed_libs.txt # Optionally add --upgrade to install latest, not fixed versions
edited 2 days ago
answered 2 days ago
ShadowRangerShadowRanger
61.9k55897
61.9k55897
add a comment |
add a comment |
A virtualenv includes a copy of the python binary itself, so nothing should happen. See a more detailed explanation here
virtualenv solves this problem by creating a completely isolated virtual environment for each of your programs. An environment is simply a directory that contains a complete copy of everything needed to run a Python program, including a copy of the python binary itself, a copy of the entire Python standard library, a copy of the pip installer, and (crucially) a copy of the site-packages directory mentioned above. When you install a package from PyPI using the copy of pip that's created by the virtualenv tool, it will install the package into the site-packages directory inside the virtualenv directory. You can then use it in your program just as before.
Edit (accounting for venv vs virtualenv): According to the venv-documentation, it can either copy or symlink the binary:
The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
3
It should be noted for anyone reading this that whilevenv
andvirtualenv
are similar, they are not the same package.
– Joshua Schlichting
2 days ago
Good point, edited the answer accordingly
– dorvak
2 days ago
This answer says a completely different thing than the other answers to this question, namely that it does not break when updating the system python, because it can copy it
– Ferrybig
2 days ago
@Ferrybig: That's becausevirtualenv
(third party package) andvenv
(built-in part of Python standard library) behave differently. This answer was originally answered from the perspective ofvirtualenv
(they missed the OP saying they usedvenv
). As it happens,virtualenv
still isn't guaranteed to work if the system Python goes away sincelibpython.so
isn't copied (which seems like a bug invirtualenv
, given its guarantees about standalone behavior), but it has a chance to work, wherevenv
definitely won't.
– ShadowRanger
2 days ago
add a comment |
A virtualenv includes a copy of the python binary itself, so nothing should happen. See a more detailed explanation here
virtualenv solves this problem by creating a completely isolated virtual environment for each of your programs. An environment is simply a directory that contains a complete copy of everything needed to run a Python program, including a copy of the python binary itself, a copy of the entire Python standard library, a copy of the pip installer, and (crucially) a copy of the site-packages directory mentioned above. When you install a package from PyPI using the copy of pip that's created by the virtualenv tool, it will install the package into the site-packages directory inside the virtualenv directory. You can then use it in your program just as before.
Edit (accounting for venv vs virtualenv): According to the venv-documentation, it can either copy or symlink the binary:
The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
3
It should be noted for anyone reading this that whilevenv
andvirtualenv
are similar, they are not the same package.
– Joshua Schlichting
2 days ago
Good point, edited the answer accordingly
– dorvak
2 days ago
This answer says a completely different thing than the other answers to this question, namely that it does not break when updating the system python, because it can copy it
– Ferrybig
2 days ago
@Ferrybig: That's becausevirtualenv
(third party package) andvenv
(built-in part of Python standard library) behave differently. This answer was originally answered from the perspective ofvirtualenv
(they missed the OP saying they usedvenv
). As it happens,virtualenv
still isn't guaranteed to work if the system Python goes away sincelibpython.so
isn't copied (which seems like a bug invirtualenv
, given its guarantees about standalone behavior), but it has a chance to work, wherevenv
definitely won't.
– ShadowRanger
2 days ago
add a comment |
A virtualenv includes a copy of the python binary itself, so nothing should happen. See a more detailed explanation here
virtualenv solves this problem by creating a completely isolated virtual environment for each of your programs. An environment is simply a directory that contains a complete copy of everything needed to run a Python program, including a copy of the python binary itself, a copy of the entire Python standard library, a copy of the pip installer, and (crucially) a copy of the site-packages directory mentioned above. When you install a package from PyPI using the copy of pip that's created by the virtualenv tool, it will install the package into the site-packages directory inside the virtualenv directory. You can then use it in your program just as before.
Edit (accounting for venv vs virtualenv): According to the venv-documentation, it can either copy or symlink the binary:
The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
A virtualenv includes a copy of the python binary itself, so nothing should happen. See a more detailed explanation here
virtualenv solves this problem by creating a completely isolated virtual environment for each of your programs. An environment is simply a directory that contains a complete copy of everything needed to run a Python program, including a copy of the python binary itself, a copy of the entire Python standard library, a copy of the pip installer, and (crucially) a copy of the site-packages directory mentioned above. When you install a package from PyPI using the copy of pip that's created by the virtualenv tool, it will install the package into the site-packages directory inside the virtualenv directory. You can then use it in your program just as before.
Edit (accounting for venv vs virtualenv): According to the venv-documentation, it can either copy or symlink the binary:
The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
edited 2 days ago
answered 2 days ago
dorvakdorvak
4,38422438
4,38422438
3
It should be noted for anyone reading this that whilevenv
andvirtualenv
are similar, they are not the same package.
– Joshua Schlichting
2 days ago
Good point, edited the answer accordingly
– dorvak
2 days ago
This answer says a completely different thing than the other answers to this question, namely that it does not break when updating the system python, because it can copy it
– Ferrybig
2 days ago
@Ferrybig: That's becausevirtualenv
(third party package) andvenv
(built-in part of Python standard library) behave differently. This answer was originally answered from the perspective ofvirtualenv
(they missed the OP saying they usedvenv
). As it happens,virtualenv
still isn't guaranteed to work if the system Python goes away sincelibpython.so
isn't copied (which seems like a bug invirtualenv
, given its guarantees about standalone behavior), but it has a chance to work, wherevenv
definitely won't.
– ShadowRanger
2 days ago
add a comment |
3
It should be noted for anyone reading this that whilevenv
andvirtualenv
are similar, they are not the same package.
– Joshua Schlichting
2 days ago
Good point, edited the answer accordingly
– dorvak
2 days ago
This answer says a completely different thing than the other answers to this question, namely that it does not break when updating the system python, because it can copy it
– Ferrybig
2 days ago
@Ferrybig: That's becausevirtualenv
(third party package) andvenv
(built-in part of Python standard library) behave differently. This answer was originally answered from the perspective ofvirtualenv
(they missed the OP saying they usedvenv
). As it happens,virtualenv
still isn't guaranteed to work if the system Python goes away sincelibpython.so
isn't copied (which seems like a bug invirtualenv
, given its guarantees about standalone behavior), but it has a chance to work, wherevenv
definitely won't.
– ShadowRanger
2 days ago
3
3
It should be noted for anyone reading this that while
venv
and virtualenv
are similar, they are not the same package.– Joshua Schlichting
2 days ago
It should be noted for anyone reading this that while
venv
and virtualenv
are similar, they are not the same package.– Joshua Schlichting
2 days ago
Good point, edited the answer accordingly
– dorvak
2 days ago
Good point, edited the answer accordingly
– dorvak
2 days ago
This answer says a completely different thing than the other answers to this question, namely that it does not break when updating the system python, because it can copy it
– Ferrybig
2 days ago
This answer says a completely different thing than the other answers to this question, namely that it does not break when updating the system python, because it can copy it
– Ferrybig
2 days ago
@Ferrybig: That's because
virtualenv
(third party package) and venv
(built-in part of Python standard library) behave differently. This answer was originally answered from the perspective of virtualenv
(they missed the OP saying they used venv
). As it happens, virtualenv
still isn't guaranteed to work if the system Python goes away since libpython.so
isn't copied (which seems like a bug in virtualenv
, given its guarantees about standalone behavior), but it has a chance to work, where venv
definitely won't.– ShadowRanger
2 days ago
@Ferrybig: That's because
virtualenv
(third party package) and venv
(built-in part of Python standard library) behave differently. This answer was originally answered from the perspective of virtualenv
(they missed the OP saying they used venv
). As it happens, virtualenv
still isn't guaranteed to work if the system Python goes away since libpython.so
isn't copied (which seems like a bug in virtualenv
, given its guarantees about standalone behavior), but it has a chance to work, where venv
definitely won't.– ShadowRanger
2 days ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55025873%2fconsequences-for-virtual-env-when-systems-python-is-removed-and-or-updated%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Note that you can safely install multiple versions of Python side by side. There is no technical reason to remove your Python 3.7 installation before installing 3.8.
– Code-Apprentice
2 days ago
@Code-Apprentice: Well, depends on how you install it. I'd strongly recommend using
make altinstall
when installing 3.8 from source, so the system defaultpython3
remains whatever shipped with the OS and any scripts shipped with the OS that havepython3
dependencies don't suddenly have Python upgraded under them in a potentially breaking way (it's rare, but there are scripts/modules that depend on internals of a given minor release and break in subsequent releases).– ShadowRanger
2 days ago