Generating random integer in python fails using random library - python

Below is the snippet:
C:\APPS>python
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'seed'
>>> random.randint(0,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'randint'
>>>

You probably have a file called random.py in the APPS directory. Delete or rename that file.

Try something like this to see which random module you're using:
In [1]: import random
In [2]: print(random.__file__)
/usr/lib/python3.2/random.py

Related

Module attribute mysteriously appears

$ python
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'request'
>>> urllib.request
<module 'urllib.request' from '/usr/lib/python3.8/urllib/request.py'>
Why is urllib.request recognized as an attribute the second time I try to access it?

import ldap3 error File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.9/dist-packages/ldap3/__init__.py", line 255, in <module>

I have installed ldap3 module for my python3. When I try to run import ldap3, I get such error. Can someone please advice me what to do?
python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ldap3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/dist-packages/ldap3/__init__.py", line 255, in <module>
from .core.connection import Connection
File "/usr/local/lib/python3.9/dist-packages/ldap3/core/connection.py", line 53
from ..strategy.async import AsyncStrategy
^
SyntaxError: invalid syntax
>>>

'module' object has no attribute 'getusersitepackages'

I am going through the python tutorial and seem to be missing something very basic. The tutorial has:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
But I get the following:
$ python
Python 2.6.8 (unknown, Jun 9 2012, 11:30:32)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getusersitepackages()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getusersitepackages'
As it says above, I am running Python 2.6.8 on cygwin.
Is this installation error?
According to the docs, this feature/function was added in python2.7 -- You're still on python 2.6

random module doesn't have randint() function

When I call random.randint(), I get the following error. Can anyone please explain what's happening?
$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "random.py", line 4, in <module>
print >>file,random.randint(100,10000)
AttributeError: 'module' object has no attribute 'randint'
>>> random.randint(100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'random' is not defined
You've called a script "random.py". Rename it so that it isn't shadowing the stdlib module of the same name.

Python's logging module misses "captureWarnings" function

Python's standard logging module is supposed to contain a useful captureWarnings function that allows integration between the logging and the warnings modules. However, it seems that my installation misses this function:
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.captureWarnings
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'captureWarnings'
>>> import logging.captureWarnings
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named captureWarnings
>>> import warnings
>>> import logging.captureWarnings
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named captureWarnings
>>>
What am I doing wrong?
Unfortunately, there is no such method in Python 2.6.5's logging module. You need Python 2.7.

Categories

Resources