Categories
Python Answers

How to fix urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error with Python?

Sometimes, we want to fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python.

In this article, we’ll look at how to fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python.

How to fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python?

To fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python, we can work around the problem with SSLContext.

The workaround bypasses the verification of the certificate.

For instance, we write

url = "https://example.com"
req = urllib2.Request(url)
gcontext = ssl.SSLContext()
info = urllib2.urlopen(req, context=gcontext).read()

to create a SSLContext instance with

gcontext = ssl.SSLContext()

Then we call urlopen with gcontext to make a GET request to the url.

Conclusion

To fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python, we can work around the problem with SSLContext.

The workaround bypasses the verification of the certificate.

Categories
Python Answers

How to install packages offline with Python?

Sometimes, we want to install packages offline with Python

In this article, we’ll look at how to install packages offline with Python.

How to install packages offline with Python?

To install packages offline with Python, we can download the packages on the computer that has Internet.

And then we can use pip install to install the packages offline.

For instance, we run

pip download -r requirements.txt

on the computer that has access to internet.

Then on the computer that has no access to internet, we run

pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt

to install the packages from the dierctory specified after --find-links.

Conclusion

To install packages offline with Python, we can download the packages on the computer that has Internet.

And then we can use pip install to install the packages offline.

Categories
Python Answers

How to read a large file – line by line with Python?

Sometimes, we want to read a large file – line by line with Python.

in this article, we’ll look at how to read a large file – line by line with Python.

How to read a large file – line by line with Python?

To read a large file – line by line with Python, we can use with, open and a for loop.

For instance, we write

with open(path) as f:
    for line in f:
        # ...

to open the file at path and assign it to f.

Then we loop through each line of f and run the code we want for each line in the loop.

with will close the file once reading is done.

Conclusion

To read a large file – line by line with Python, we can use with, open and a for loop.

Categories
Python Answers

How to import a module from a relative path with Python?

Sometimes, we want to import a module from a relative path with Python.

In this article, we’ll look at how to import a module from a relative path with Python.

How to import a module from a relative path with Python?

To import a module from a relative path with Python, we can use the sys.path.insert method to register the path of the module.

For instance, we write

import sys
sys.path.insert(0, module_path)
import Bar

to call sys.path.insert to register module_path as a module directory.

Then we import Bar from module_path with

import Bar

Conclusion

To import a module from a relative path with Python, we can use the sys.path.insert method to register the path of the module.

Categories
Python Answers

How to check if a given key already exists in a dictionary with Python?

Sometimes, we want to check if a given key already exists in a dictionary with Python.

In this article, we’ll look at how to check if a given key already exists in a dictionary with Python.

How to check if a given key already exists in a dictionary with Python?

To check if a given key already exists in a dictionary with Python, we can use the in operator.

For instance, we write

d = {"key1": 10, "key2": 23}

if "key1" in d:
    print("yes")

to check if key1 is a key in dict d.

Since key1 is in d, 'yes' will be printed.

Conclusion

To check if a given key already exists in a dictionary with Python, we can use the in operator.