Python deployments: Difference between revisions
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight" Tags: Mobile edit Mobile web edit |
|||
Line 22: | Line 22: | ||
See Digital Ocean's quick guide to [https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-ubuntu-16-04 setting up a local Python programming environment], including [https://virtualenv.pypa.io/en/stable/ virtual environments], which is what I did for [https://github.com/Miserlou/SoundScrape SoundScrape] Btw, SoundScrape is a neat tool to download sound files from SoundCloud. | See Digital Ocean's quick guide to [https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-ubuntu-16-04 setting up a local Python programming environment], including [https://virtualenv.pypa.io/en/stable/ virtual environments], which is what I did for [https://github.com/Miserlou/SoundScrape SoundScrape] Btw, SoundScrape is a neat tool to download sound files from SoundCloud. | ||
<ref> | <ref> | ||
< | <syntaxhighlight lang="bash"> | ||
cd | cd | ||
mkdir environments | mkdir environments | ||
Line 32: | Line 32: | ||
pip install soundscrape --upgrade | pip install soundscrape --upgrade | ||
soundscrape https://soundcloud.com/pianoman_weddings/coldplay | soundscrape https://soundcloud.com/pianoman_weddings/coldplay | ||
</ | </syntaxhighlight> | ||
</ref> | </ref> | ||
Line 46: | Line 46: | ||
As an example, I needed to create a Python Virtual Environment to use the [https://github.com/freephile/meza/issues/63#issuecomment-2020431602 Ansible Playbook Grapher] | As an example, I needed to create a Python Virtual Environment to use the [https://github.com/freephile/meza/issues/63#issuecomment-2020431602 Ansible Playbook Grapher] | ||
< | <syntaxhighlight lang="bash"> | ||
python3.11 -m venv myenv | python3.11 -m venv myenv | ||
# .. some output .. | # .. some output .. | ||
Line 56: | Line 56: | ||
# done using this python? | # done using this python? | ||
deactivate | deactivate | ||
</ | </syntaxhighlight> | ||
You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation. | You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation. |
Latest revision as of 13:27, 24 February 2025
Check Python
Check the version of the default Python interpreter:
python --version
List the versions of python available:
ls /usr/bin/python*
Use update-alternatives
to setup system-wide ability to choose Python interpreter. The one with the higher priority number will become the default.
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
Now we can list the choices:
update-alternatives --list python
And choose one:
update-alternatives --config python
We can also remove a choice if it's no longer an option on the system:
update-alternatives --remove python /usr/bin/python2.7
Python Virtual Environments
See Digital Ocean's quick guide to setting up a local Python programming environment, including virtual environments, which is what I did for SoundScrape Btw, SoundScrape is a neat tool to download sound files from SoundCloud. [1]
Hynek Schlawack (from 2013)
Python Packaging
In the old days (2015) there were still debates about how to package Python and install stuff. Now, it's settled. Use pip. Easy_install is dead. See the docs. Also note that venv
is the successor to virtualenv
Basic PIP and Virtual Environments
Do NOT (normally) use sudo with pip. Use a virtual environment. As of Python 3.4, the command is now called pyvenv
or simply venv
. As of Python 3.6 pyvenv
is deprecated in favor of using python3 -m venv
to help prevent any potential confusion as to which Python interpreter a virtual environment will be based on. python3 is not generic. It represents the exact version you wish to use in your virtual environment.
As an example, I needed to create a Python Virtual Environment to use the Ansible Playbook Grapher
python3.11 -m venv myenv
# .. some output ..
source myenv/bin/activate
# maybe upgrade pip
pip install --upgrade pip
# then install your local environment packages
pip install what-i-want
# done using this python?
deactivate
You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation.
It is best to use a virtual environment which isolates packages for you. That way you can play around without polluting the global python install.
As a bonus, virtualenv does not need elevated permissions.
- Installing a package is as simple as
pip install foo
- Upgrades are
pip install --upgrade foo
pip uninstall foo
if you want to remove foo
References
- ↑
cd mkdir environments cd environments/ pyvenv my_env source my_env/bin/activate pip install --upgrade pip pip install soundscrape pip install soundscrape --upgrade soundscrape https://soundcloud.com/pianoman_weddings/coldplay