3.12.3
)$ sudo apt install python3 python3-dev python3-pip python3-venvRed Hat:
$ sudo dnf install python3 python3-devel python3-pip python3-venvYou can also install Python from source: https://devguide.python.org/getting-started/setup-building/
$ python3 --version
Python 3.12.3
$ pip --version
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)Run a simple script from the command line:
$ python3 -c "print('hello python')"
hello pythonCreate and run a simple script:
$ echo "print('Hello Python')" > hello.py
$ python3 hello.py
Hello PythonAccessing Python Shell:
$ python3
Python 3.12.3 (main, Feb 4 2025, 14:48:35) [GCC 13.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print('Hello Python') Hello Python >>> quit()
$ mkdir -p ~/dev/python/my_project $ cd ~/dev/python/my_projectCreate virtual environment:
$ python3 -m venv .venvDirectory structure:
$ tree -L 1 .venv
.venv/ ├── bin ├── include ├── lib └── pyvenv.cfgPython project structure:
my_project/ ├── .venv/ # virtual environment ├── src/ │ └── my_package/ │ ├── __init__.py │ └── main.py ├── tests/ │ ├── __init__.py │ └── test_main.py ├── docs/ ├── .gitignore # exclude .venv, __pycache__, ... ├── README.md ├── requirements.txt # production dependencies ├── requirements-dev.txt # development dependencies ├── pytest.ini # pytest configuration ├── setup.py # package configuration └── pyproject.toml # packaging tools configuration
$ source .venv/bin/activate (.venv) $When the virtual environment is active, you will see the name of the environment in parentheses ("(.venv)").
(.venv) $ which python ~/dev/python/my_project/.venv/bin/python
(.venv) $ python3 --version Python 3.12.3
(.venv) $ pip list Package Version ------- ------- pip 24.0
(.venv) $ deactivate $You can also deactivate the virtual environment by closing the terminal it's running in.
$ rm -rf ~/dev/python/my_project/.venv/
$ python3 -m pip install package-nameUse "--user" to install the package for the current user only:
$ python3 -m pip install --user package-nameInstall a specific version of a package:
$ python3 -m pip install package-name==package-versionInstall packages from requirements file:
$ python3 -m pip install -r requirements.txtList installed packages:
$ python3 -m pip listShow package information:
$ python3 -m pip show package-nameUpgrade a package:
$ python3 -m pip install --upgrade package-nameUpgrade pip itself:
$ python3 -m pip install --upgrade pipUninstall a package:
$ python3 -m pip uninstall package-nameCreate a requirements file:
$ python3 -m pip freeze > requirements.txt
$ python
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>>