Python error: AttributeError: ‘module’ object has no attribute

When you import lib, you’re importing the package. The only file to get evaluated and run in this case is the 0 byte __init__.py in the lib directory. If you want access to your function, you can do something like this from lib.mod1 import mod1 and then run the mod12 function like so mod1.mod12(). If you want to be able to access mod1 when you … Read more

ValueError: need more than 1 value to unpack python

This means that there is a line in packages.txt that, when you strip whitespace and split on commas, doesn’t give exactly three pieces. In fact, it seems that it gives only 1 piece (“need more than 1 value to unpack”), which suggests that there’s a line with no commas at all. Perhaps there are blank or comment … Read more

Python giving FileNotFoundError for file name returned by os.listdir

It is because os.listdir does not return the full path to the file, only the filename part; that is ‘foo.txt’, when open would want ‘E:/somedir/foo.txt’ because the file does not exist in the current directory. Use os.path.join to prepend the directory to your filename: (Also, you are not closing the file; the with block will take care of it automatically).

‘int’ object has no attribute ‘__getitem__’

The error: means that you’re attempting to apply the index operator [] on an int, not a list. So is col not a list, even when it should be? Let’s start from that. Look here: Use a different variable name inside, looks like the list comprehension overwrites the col variable during iteration. (Not during the iteration when you set col, but during the following … Read more

How can I convert radians to degrees with Python?

Python includes two functions in the math package; radians converts degrees to radians, and degrees converts radians to degrees. To match the output of your calculator you need: Note that all of the trig functions convert between an angle and the ratio of two sides of a triangle. cos, sin, and tan take an angle in radians as input and return … Read more

How to sort a set in python?

You can’t Just read python documentation: Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. Emphasis mine. That means you will never be able to sort … Read more