Planet Python

Subscribe to Planet Python feed
Planet Python -
Updated: 22 hours 2 min ago

Mike Driscoll: uv – Python’s Fastest Package Installer and Resolver

Tue, 2024-02-27 09:09

There’s a new Python package installer out now and it’s called uv. The uv package installer and resolver is made by Astral. Uv is written in Rust instead of Python and is super fast! Astral is best known for Python’s fastest formatter, Ruff. The uv package is meant to be a drop-in replacement for pip and pip-tools. According to Astral, “uv is 8-10x faster than pip and pip-tools without caching, and 80-115x faster when running with a warm cache (e.g., recreating a virtual environment or updating a dependency)”.

Astral is also taking over the development of Rye, an experimental Python packaging tool from Armin Ronacher. From the sounds of Astral’s announcement, Rye and uv will become one tool as the two projects have a shared vision for Python packaging.

Installing uv

You can install uv using Curl:

curl -LsSf | sh

Or you can use pip:

pip install uv

Now that you have uv installed, you can start installing packages!

Using uv

Let’s try running uv in your terminal:

c:\code> uv Usage: uv.exe [OPTIONS] <COMMAND> Commands: pip Resolve and install Python packages venv Create a virtual environment cache Manage the cache help Print this message or the help of the given subcommand(s) Options: -q, --quiet Do not print any output -v, --verbose Use verbose output --color <COLOR> Control colors in output [default: auto] [possible values: auto, always, never] -n, --no-cache Avoid reading from or writing to the cache [env: UV_NO_CACHE=] --cache-dir <CACHE_DIR> Path to the cache directory [env: UV_CACHE_DIR=] -h, --help Print help (see more with '--help') -V, --version Print version

You’ll need to create and activate a Python virtual environment to install packages with uv.

Here’s an example:

C:\code> uv venv test Using Python 3.11.5 interpreter at C:\Users\wheifrd\AppData\Local\Programs\Python\Python311\python.exe Creating virtualenv at: test Activate with: test\Scripts\activate C:\code> .\test\Scripts\activate (test) C:\books>

Now you’re ready to install a Python package. You can use numpy for a test run:

(test) C:\books> uv pip install numpy Resolved 1 package in 615ms Downloaded 1 package in 2.81s Installed 1 package in 332ms + numpy==1.26.4

As you might expect, you can also use uv to install:

  • a list of space-delimited packages
  • a requirements.txt file
  • a pyproject.toml file

If you need to generate a locked requirements.txt file, you can run uv pip compile.

Wrapping Up

Astral hopes to create a “Cargo for Python” with the release of uv. While it’s still early, this project is well worth watching as the Rust package itself is amazingly fast and useful even though it’s only been out for about a year. You can read more about uv in Astral’s blog post.


The post uv – Python’s Fastest Package Installer and Resolver appeared first on Mouse Vs Python.

Categories: FLOSS Project Planets

Real Python: Python Basics Exercises: Installing Packages With pip

Tue, 2024-02-27 09:00

So far on the Python Basics learning path, you’ve been working within the bounds of the Python standard library. Now it’s time to unlock packages that aren’t included with Python by default. To do that, you’ll need pip.

Many programming languages offer a package manager that automates the process of installing, upgrading, and removing third-party packages. Python is no exception. The de facto package manager for Python is called pip.

In this Python Basics Exercises course, you’ll test and reinforce your knowledge of installing packages and managing virtual environments.

In this video course, you’ll practice:

  • Installing and managing third-party packages with pip
  • Using virtual environments to separate project dependencies
  • Declaring requirements and re-create a development environment

By the end of this course, you’ll have an even stronger grasp of installing packages to suit your programming needs

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3. Note that you’ll be using IDLE to interact with Python throughout this course.

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Categories: FLOSS Project Planets

Python⇒Speed: Jevons Paradox doesn't always apply to software

Mon, 2024-02-26 19:00

When it comes to fighting climate change, I strongly believe that getting involved in politics is one of the most useful things you can do. But given how energy-intensive software is these days, writing more efficient software also seems worth doing, especially if your software is used at scale.

However, whenever efficiency is brought up, Jevons Paradox rears its head. Writing in the 19th century, Jevons pointed out that increased efficiency in the use of coal didn’t decrease the amount of coal used, it actually increased it. As it became more cost-effective and more useful, coal ended up being used in far more places.

So what about software: will faster software result in more usage, completely undoing the energy-consumption benefits of efficiency?

Categories: FLOSS Project Planets

PyCharm: PyCharm 2023.3.4: AI Assistant’s Efficiency Boosters and a more powerful Django Structure tool window

Mon, 2024-02-26 16:10

This year, we are trying out a new approach with our releases, moving away from a quarterly schedule to more regular monthly feature-rich releases. This change is intended to deliver new features more rapidly and streamline the feedback process.

Upgrade to PyCharm 2023.3.4

New AI Assistant functionality [PyCharm Professional]

Get work done faster with new AI Assistant actions. Generate Python type annotations for functions and get Django views, admins, and serializers for models. Obtain a quick analysis of the data inside your DataFrame and convert Jupyter notebooks to scripts. Do all of this with just a click!

The Django Structure tool window [PyCharm Professional]

Navigate through your Django projects faster with the new features of the Django Structure tool window. Access or identify and register unregistered admins directly in the tool window.

Quick Documentation for type parameter declarations in the new syntax

If you have already adopted the new syntax for type parameter declarations introduced in PEP 695, this feature will definitely make your life easier. The Quick Documentation popup clearly explains the type parameters and type aliases used in your code.

These are the most notable updates featured in the PyCharm 2023.3.4 release. For a detailed overview of all the changes, we recommend reviewing the release notes.

Learn more about PyCharm 2023.3.4

Your feedback is invaluable to us as we work to improve PyCharm. We encourage you to share your thoughts and suggestions on the latest features and updates. Connect with us on X (formerly Twitter) or drop a comment below to let us know what you think. If you come across any bugs while working with the IDE, please report them to our issue tracker.

Categories: FLOSS Project Planets

Real Python: Duck Typing in Python: Writing Flexible and Decoupled Code

Mon, 2024-02-26 09:00

Python makes extensive use of a type system known as duck typing. The system is based on objects’ behaviors and interfaces. Many built-in classes and tools support this type system, which makes them pretty flexible and decoupled.

Duck typing is a core concept in Python. Learning about the topic will help you understand how the language works and, more importantly, how to use this approach in your own code.

In this tutorial, you’ll learn:

  • What duck typing is and what its pros and cons are
  • How Python’s classes and tools take advantage of duck typing
  • How special methods and protocols support duck typing
  • What alternatives to duck typing you’ll have in Python

To get the most out of this tutorial, you should be familiar with several Python concepts, including object-oriented programming, classes, special methods, inheritance, and interfaces.

Get Your Code: Click here to download the free sample code that shows you how to use duck typing in Python.

Getting to Know Duck Typing in Python

In object-oriented programming, classes mainly aim to encapsulate data and behaviors. Following this idea, you can replace any object with another if the replacement provides the same behaviors. This is true even if the implementation of the underlying behavior is radically different.

The code that uses the behaviors will work no matter what object provides it. This principle is the basis of a type system known as duck typing.

Duck Typing: Behaving Like a Duck

You’ll find many different definitions of duck typing out there. At its core, this coding style is based on a well-known saying:

If it walks like a duck and it quacks like a duck, then it must be a duck.

Extrapolating this to programming, you can have objects that quack like a duck and walk like a duck rather than asking whether those objects are ducks. In this context, quack and walk represent specific behaviors, which are part of the objects’ public interface (API).

Duck typing is a type system where an object is considered compatible with a given type if it has all the methods and attributes that the type requires. This type system supports the ability to use objects of independent and decoupled classes in a specific context as long as they adhere to some common interface.

Duck typing is pretty popular in Python. The language documentation defines duck typing as shown below:

A programming style which does not look at an object’s type to determine if it has the right interface; instead, the method or attribute is simply called or used (“If it looks like a duck and quacks like a duck, it must be a duck.”) By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution.

Duck-typing avoids tests using type() or isinstance(). (Note, however, that duck-typing can be complemented with abstract base classes.) Instead, it typically employs hasattr() tests or EAFP programming. (Source)

Here’s a quick example that involves birds that can swim and fly:

Python class Duck: def swim(self): print("The duck is swimming") def fly(self): print("The duck is flying") class Swan: def swim(self): print("The swan is swimming") def fly(self): print("The swan is flying") class Albatross: def swim(self): print("The albatross is swimming") def fly(self): print("The albatross is flying") Copied!

In this example, your three birds can swim and fly. However, they’re completely independent classes. Because they share the same interface, you can use them in a flexible manner:

Python >>> from birds_v1 import Duck, Swan, Albatross >>> birds = [Duck(), Swan(), Albatross()] >>> for bird in birds: ... ... bird.swim() ... The duck is flying The duck is swimming The swan is flying The swan is swimming The albatross is flying The albatross is swimming Copied!

Python doesn’t care about what object bird is holding at a given time. It just calls the expected methods. If the object provides the method, then the code works without breaking. That’s the flexibility that duck typing offers.

The duck typing system is pretty popular in Python. In most cases, you shouldn’t worry about making sure that an object is of the right type for using it in a certain piece of code. Instead, you can rely on objects that quack like ducks and walk like ducks.

Duck Typing and Polymorphism

In object-oriented programming, polymorphism allows you to treat objects of different types as the same general type. Polymorphism aims to enable code to work with objects of various types through a uniform interface (API), which helps you write more general and reusable code.

You’ll find different forms of polymorphism in object-oriented programming. Duck typing is one of them.

Read the full article at »

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Categories: FLOSS Project Planets