Categories
Python Answers

How to fix pip install failing with: OSError: [Errno 13] Permission denied on directory with Python?

To fix pip install failing with: OSError: [Errno 13] Permission denied on directory with Python, we can create a virtualenv in a folder where we have permission to write files.

To do this, we run

virtualenv venv
source venv/bin/activate
pip install -r requirements.txt

where venv is a folder that we can write files to.

And then we activate the virtualenv with

source venv/bin/activate

And then we install the packages into the virtualenv with

pip install -r requirements.txt

We can also install the packages into our home directory with

pip install --user -r requirements.txt
Categories
Python Answers

How to check if string input is a number with Python?

Sometimes, we want to check if string input is a number with Python.

In this article, we’ll look at how to check if string input is a number with Python.

How to check if string input is a number with Python?

To check if string input is a number with Python, we can try to parse the string into a number.

For instance, we write

try:
    val = int(user_input)
except ValueError:
    print("Not an int")

to parse user_input into an int with int.

If it can’t be parsed into an int, a ValueError will be raised.

And we catch it and print 'Not an int' if that’s the case.

Conclusion

To check if string input is a number with Python, we can try to parse the string into a number.

Categories
Python Answers

How to create an ordered set with Python?

Sometimes, we want to create an ordered set with Python.

In this article, we’ll look at how to create an ordered set with Python.

How to create an ordered set with Python?

To create an ordered set with Python, we can create a list from dict keys.

For instance, we write

keywords = ['foo', 'bar', 'bar', 'foo', 'baz', 'foo']
l = list(dict.fromkeys(keywords))

to get a dict with keys created from the keywords list.

And then we can convert the dict returned to a list with list.

As a result, we get ['foo', 'bar', 'baz'] as the value of l.

Conclusion

To create an ordered set with Python, we can create a list from dict keys.

Categories
Python Answers

How to use Python dictionary comprehension?

Sometimes, we want to use Python dictionary comprehension.

In this article, we’ll look at how to use Python dictionary comprehension.

How to use Python dictionary comprehension?

To use Python dictionary comprehension, we can use it to map a dict’s keys and values to new values.

For instance, we write

d = {n: n**2 for n in range(5)}

to create a dictionary with key n and value with n squared.

And we create entries with n from 0 to 4 with range(5).

Conclusion

To use Python dictionary comprehension, we can use it to map a dict’s keys and values to new values.

Categories
Python Answers

How to reverse or invert a dictionary mapping with Python?

Sometimes, we want to reverse or invert a dictionary mapping with Python.

In this article, we’ll look at how to reverse or invert a dictionary mapping with Python.

How to reverse or invert a dictionary mapping with Python?

To reverse or invert a dictionary mapping with Python, we can use dictionary comprehension.

For instance, we write

inv_map = {v: k for k, v in my_map.items()}

to flip the key k and value v with v: k for k, v in my_map.items().

We get the key-value pairs with items.

Conclusion

To reverse or invert a dictionary mapping with Python, we can use dictionary comprehension.