Unable to import openpyxl module [closed] - python-3.x

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The Code below works just fine :
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
import os
os.chdir('c:\\users\\shivam\\Desktop')
wb = openpyxl.load_workbook('data.xlsx')
print(wb.sheetnames)
print(get_column_letter(27))
but when I try to import it as:
from openpyxl import *
import os
os.chdir('c:\\users\\shivam\\Desktop')
wb = openpyxl.load_workbook('data.xlsx')
print(wb.sheetnames)
print(get_column_letter(27))
it gives an error:
Traceback (most recent call last):
File "D:/python projects/excel.py", line 4, in <module>
wb = openpyxl.load_workbook('data.xlsx')
NameError: name 'openpyxl' is not defined
Why everything from the module is not getting imported all at once?

If you import a module like import openpyxl, then you should use openpyxl.load_workbook.
But while importing a module like from openpyxl import *, rather than importing the whole module you're importing all the classes and functions present inside the module, so there is no need of calling openpyxl.something(), just directly call the name of class or function like load_workbook

Related

error in python code while creating an object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
An error is coming while declaring an object . Any suggestions would be appreciated.
from tkinter import*
class Bill_App:
def _init_(self,root):
pass
root=Tk()
obj = Bill_App(root)
root.mainloop()
error:-
Traceback (most recent call last):
File "C:\Users\rish6\AppData\Local\Programs\Python\Python37\Bill_App\Bill.py", line 9, in <module>
obj = Bill_App(root)
TypeError: Bill_App() takes no arguments
Error is coming due to wrong syntax of init method.
def _init_(self,root):
it should be __init__ -> def __init__(self,root):
Changed code
from tkinter import*
class Bill_App:
def __init__(self,root):
print('object is created'+str(root))
root=Tk()
obj = Bill_App(root)
root.mainloop()

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.

Can't figure out how to solve this circular import [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I was splitting some functions into a separate file. Found the functions and helper functions. Set all the paths and import in other files so everything can still be called where necessary. But after removing an unused import from the original file it gives a circular import error it seems.
So there is an unused import and while there, every test (524) succeeds, but the branch cant be pushed because of the unused import. If we remove the import, the tests goes into circular import I think and wont start at all.
The first thing i tried is to change the regarding import statements from: from /module import /object --> import module
This made the tests start but everything went wrong after that.
After that I tried to find a way through the circular import path but I just lost track of everything while I dont know if it is even a solution.
`
Traceback (most recent call last):
File "/home/user/../.py", line 2, in <module>
from x.model import x
File "/home/user/../.py", line 1, in <module>
import A
File "/home/user/../.py", line 4, in <module>
import B
File "/home/user/../.py", line 1, in <module>
import C
File "/home/user/../.py", line 9, in <module>
import D
File "/home/user/../.py", line 4, in <module>
import E
File "/home/user/../.py", line 9, in <module>
from x.model import x
ImportError: cannot import name 'x'
`
If you are using imported Ive in /home/user/projects/.../nsx/utils.py file, inside some method or function, then you should remove import statement (i.e from ive.models import Ive) from top of file import statements(if you are doing so), and import it dynamically means put import statement at the place where you are using Ive inside the method or function.
# Assume `/home/user/projects/.../nsx/utils.py` file
# Don't import Ive here
# from ive.models import Ive
def func():
# import here
from ive.models import Ive
# use Ive here

How to fix 'NameError: name 'field' is not defined' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
In my attempt to create a class dots with the fields n and xy as shown below:
from dataclasses import dataclass
#dataclass
class dots:
n: int = 200
xy: List[int] = field(default_factory=list)
I am constantly getting the error :
NameError: name 'field' is not defined
Any ideas on how to fix it?
My operating system is Ubuntu 18.04.3 LTS, and the kernel version 4.15.0-58-generic. I am using Python 3.6.4
You need to import field() to use it in your code:
from dataclasses import dataclass, field

urlopen is not working for python3 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to fetch data from a url. I have tried the following in Python 2.7:
import urllib2 as ul
response = ul.urlopen("http://in.bookmyshow.com/")
page_content = response.read()
print page_content
This is working fine. But when i try it in Python 3.4 it is throwing an error:
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
I am using:
import urllib.request
response = urllib.request.urlopen('http://in.bookmyshow.com/')
data = response.read()
print data
It works for me (Python 3.4.3). You need to use print(data) in Python 3.
As a side note you may also want to consider requests which makes it way easier to interact via HTTP(S).
>>> import requests
>>> r = requests.get('http://in.bookmyshow.com/')
>>> r.ok
True
>>> plaintext = r.text
Finally, if you want to get data from such complicated pages (which are intended to be displayed, as opposed to an API), you should have a look at Scrapy which will make your life easier as well.

Resources