AttributeError: 'module' object has no attribute 'randint' [duplicate] - python-3.x

This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 3 years ago.
I have a recent problem with random module
I dont understand why is that?
I showed the exact path that goes to random.py but it still didnt work.
python3.4 64bit
using pycharm 2016.2.2
First group is real error
Second group relates to help(random)
Sorry about confusion.
File "C:/Users/blueg/Google Drive/Programming/Python/PycharmProjects/LearningPython/Random Module/random.py", line 1, in <module>
import random File "C:\Users\blueg\Google Drive\Programming\Python\PycharmProjects\LearningPython\Random Module\random.py", line 4, in <module>
a = random.randint(1,6) AttributeError: 'module' object has no attribute 'randint'
Help on module random:
NAME random
FILE c:\users\blueg\google drive\programming\python\pycharmprojects\learningpython\random module\random.py

you have called your module random.py and thus you're importing your own module: rename it it will work (and also delete the random.pyc associated file or python will use that one in turn)
(I sometimes think that naming one's module in german, french or other avoids conflicts :))

Related

from sparkdl import DeepImageFeaturizer

I need to use spark in transfer learning to train images ,the error is:
"nnot import name 'resnet50' from 'keras.applications' (/usr/local/lib/python3.7/dist-packages/keras/applications/init.py) "
i try to solve this question since one week, this one is coming from sparkdl, if you add to this file (sparkdl/transformers/keras_applications.py)
**
from tensorflow.keras.applications
**, it will be return normal, but this time you will see another error like
AttributeError: module 'tensorflow' has no attribute 'Session'
i tried on different IDE (Pycharm, Vs Code) but i got the same errors. there are different explications on Stackoverflow. but i'm totally confused now

AttributeError: partially initialized module 'boto3' has no attribute 'session' (most likely due to a circular import)

I tried the below code
import boto3
aws_mg_con=boto3.session.Session(profile_name='demo_user')
iam_con=aws_mg_con.resource('iam')
But i receive this error
'AttributeError: partially initialized module 'boto3' has no attribute 'session' (most likely due to a circular import)'
I tried renaming the file, still same error is coming.
Need help.
There may be a local file with the same name as 'boto3' , "Python sees the local file and thinks it's the module ".
Make sure the name of the file is not the same as the module
Thanks. The issue was, i saved the code with other name, but kept the first file as 'boto3.py'.
I deleted the old file, now the code is working.

Cannot import namedtuple [duplicate]

This question already has answers here:
ImportError: cannot import name namedtuple
(2 answers)
Closed 3 years ago.
I am currently working in PYCHARM and it gives me the error whenever I run the program. I tried various methods to remove this error but to no avail.
What can I do to fix this issue?
import re
File "F:\Anaconda\envs\tensorflow\lib\re.py", line 125, in <module>
import functools
File "F:\Anaconda\envs\tensorflow\lib\functools.py", line 21, in <module>
from collections import namedtuple
ImportError: cannot import name 'namedtuple'
Check the module in stdlib named types, it gets imported instead.
You can either rename your module, or switch to absolute imports.
Hope this will help you.

AttributeError: 'module' object has no attribute 'LogicParser' with nltk.LogicParser()

I'm playing with examples from Natural Language Processing with Python and this line:
lp = nltk.LogicParser()
produces
AttributeError: 'module' object has no attribute 'LogicParser'
error message. I imported several nltk modules and I can't figure out what is missing. Any clues?
It sounds like you've spotted the problem, but just in case: You are reading the first edition of the NLTK book, but evidently you have installed NLTK 3, which has many changes. Look at the current version of chapter 10 for the correct usage.

Import parent directory for brief tests

I have searched this site top to bottom yet have not found a single way to actually accomplish what I want in Python3x. This is a simple toy app so I figured I could write some simple test cases in asserts and call it a day. It does generate reports and such so I would like to make sure my code doesn't do anything wonky upon changes.
My current directory structure is: (only relevant parts included)
project
-model
__init__.py
my_file.py
-test
my_file_test.py
I am having a hell of a time getting my_file_test.py to import my_file.py.
Like I've said. I've searched this site top to bottom and no solution has worked. My version of Python is 3.2.3 running on Fedora 17.
Previously tried attempts:
https://stackoverflow.com/questions/5078590/dynamic-imports-relative-imports-in-python-3
Importing modules from parent folder
Can anyone explain python's relative imports?
How to accomplish relative import in python
In virtually every attempt I get an error to the effect of:
ImportError: No module named *
OR
ValueError: Attempted relative import in non-package
What is going on here. I have tried every accepted answer on SO as well as all over the interwebs. Not doing anything that fancy here but as a .NET/Java/Ruby programmer this is proving to be the absolute definition of intuitiveness.
EDIT: If it matters I tried loading the class that I am trying to import in the REPL and I get the following:
>>> import datafileclass
>>> datafileclass.methods
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
>>> x = datafileclass('sample_data/sample_input.csv')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
If it matters...I know the functionality in the class works but I can't import it which in the now is causing an inability to test. In the future will certainly cause integration issues. (names changed to protect the innocent)
getting within a couple of weeks of desired functionality for this iteration of the library...any help could be useful. Would have done it in Ruby but the client wants the Python as a learning experience,
Structure your code like this:
project
-model
__init__.py
my_file.py
-tests
__init__.py
test_my_file.py
Importantly, your tests directory should also be a module directory (have an empty __init__.py file in it).
Then in test_my_file.py use from model import my_file, and from the top directory run python -m tests.test_my_file. This is invoking test_my_file as a module, which results in Python setting up its import path to include your top level.
Even better, you can use pytest or nose, and running py.test will pick up the tests automatically.
I realise this doesn't answer your question, but it's going to be a lot easier for you to work with Python standard practices rather than against them. That means structuring your project with tests in their own top-level directory.

Resources