When I run this scrip in terminal didn't work :/ , but when run that scrip on project pycharm it works without problems.
this my code :
import socket
from binascii import hexlify
def iphost():
for ip_addr in ['192.168.1.15','127.0.0.1']:
packed = socket.inet_aton(ip_addr)
unpacked = socket.inet_ntoa(packed)
print ('Ip address : %s = packed: %s , unpacked %s'%(ip_addr,hexlify(packed),unpacked))
iphost()
.
Traceback (most recent call last):
File "socket.py", line 1, in <module>
import socket
File "/home/linuxmint/Desktop/socket.py", line 9, in <module>
iphost()
File "/home/linuxmint/Desktop/socket.py", line 5, in iphost
packed = socket.inet_aton('192.168.1.15','127.0.0.1')
AttributeError: module 'socket' has no attribute 'inet_aton'
this problem when run in terminal
You named your file socket.py so now import socket loads your file socket.py instead of python module - and it can't find 'inet_aton' in your file.
Change name of your file - ie. socket-example.py
Related
I was just learning socket programming in python and was unable to run my socket commands in vs code.
the code i wrote is:-
import socket
import threading
PORT=5050
SERVER =socket.gethostbyname(socket.gethostname)
print(SERVER)
The code produces this error:
Traceback (most recent call last):
File "c:\Users\Gurkirat Singh\Desktop\tempCodeRunnerFile.py", line 1, in <module>
import socket
File "c:\Users\Gurkirat Singh\Desktop\socket.py", line 3, in <module>
from xmlrpc.client import Server
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\xmlrpc\client.py", line 136, in <module>
import http.client
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\http\client.py",
line 789, in <module>
class HTTPConnection:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\http\client.py",
line 837, in HTTPConnection
def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
AttributeError: partially initialized module 'socket' has no attribute '_GLOBAL_DEFAULT_TIMEOUT' (most likely due to a circular import)
Looking at your stack trace, you seem to have a file named socket.py:
File "c:\Users\Gurkirat Singh\Desktop\socket.py", line 3, in <module>
from xmlrpc.client import Server
Having a file named socket.py in your workspace causes import problems because Python won't import its library module socket but instead will import your own file.
Rename your file from socket.py to something else like mysock.py and delete anything from your Desktop directory called socket.pyc if it exists.
I have a little python script that checks my current IPv4 DNS Server address. It's working just fine. I use the wmi package.
Now: I use this script in Kodi. And first time it runs, it runs perfectly. But the second time I get a weird error.
Why does my code work the first time, but not the following attempts?
Hope you guys can help me. I'm lost.
my script:
import wmi
nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True);
nic = nic_configs[0];
dns = nic.DNSServerSearchOrder[0];
the error:
Error Type: <type 'exceptions.AttributeError'>
Error Contents: 'NoneType' object has no attribute 'platform'
Traceback (most recent call last):
File "C:\Python\Lib\site-packages\mytest.py", line 22, in <module>
import wmi
File "C:\Python\Lib\site-packages\wmi.py", line 88, in <module>
from win32com.client import GetObject, Dispatch
File "C:\Python\Lib\site-packages\win32com\__init__.py", line 6, in <module>
import pythoncom
File "C:\Python\Lib\site-packages\pythoncom.py", line 3, in <module>
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
File "C:\Python\Lib\site-packages\pywintypes.py", line 20, in __import_pywin32_system_module__
if not sys.platform.startswith("win32"):
AttributeError: 'NoneType' object has no attribute 'platform'
-->End of Python script error report<--
I am trying to make a script that moves all the .txt files in your desktop to desktop/org, the code is as follows:
import os
import shutil
userhome = os.path.expanduser('~')
src = userhome + '/Desktop/'
dst = src+ 'org/'
def main():
txtlist = os.listdir(src)
for file in txtlist:
sortFiles(file)
def sortFiles(file):
if file.endswith(".txt"):
shutil.move(src+file,dst)
main()
If I execute the .py I get this error: AttributeError: 'module' object has no attribute 'copy'. However, if I erase the last line "main()" and I import this script as a module in the python command line and I call .main() from there it works perfectly well. How can I make this work as a script?
Traceback (most recent call last):
File "C:\Python32\org.py", line 3, in <module>
import shutil
File "C:\Python32\lib\shutil.py", line 14, in <module>
import tarfile
File "C:\Python32\lib\tarfile.py", line 50, in <module>
import copy
File "C:\Python32\lib\copy.py", line 61, in <module>
from org.python.core import PyStringMap
File "C:\Python32\org.py", line 19, in <module>
main()
File "C:\Python32\org.py", line 12, in main
sortFiles(file)
File "C:\Python32\org.py", line 16, in sortFiles
shutil.move(src+file,dst)
AttributeError: 'module' object has no attribute 'move'
I am using python 3.2
Wow, that’s some bad luck. You can understand what’s going on when you look at the traceback:
Traceback (most recent call last):
File "C:\Python32\org.py", line 3, in <module>
import shutil
So, the first line that is being executed is import shutil. That’s where everything starts going wrong—which is suprising given that it’s a built-in module.
File "C:\Python32\lib\shutil.py", line 14, in <module>
import tarfile
File "C:\Python32\lib\tarfile.py", line 50, in <module
import copy
So shutil import tarfile, which imports copy.
File "C:\Python32\lib\copy.py", line 61, in <module>
from org.python.core import PyStringMap
And copy has this nice thing that tries to import PyStringMap from a module called org.python.core. Now, this module usually does not exist, which would cause copy to use some alternative code instead: PyStringMap = None.
The problem is, that there is something called org: Your own script, org.py. So what happens is that Python tries to find something called python.core.PyStringMap in your org.py. To be able to go that far, it needs to execute the script, including the main() call at the end:
File "C:\Python32\org.py", line 19, in <module>
main()
File "C:\Python32\org.py", line 12, in main
sortFiles(file)
File "C:\Python32\org.py", line 16, in sortFiles
shutil.copy(src+file,dst)
AttributeError: 'module' object has no attribute 'copy'
And that leads us to the shutil.copy line which is a call to the shutil module. As this is the module we are still importing (from the very first line!), its import hasn’t completely yet, so the copy function inside does not exist, causing the AttributeError.
This is a very unfortunate situation in which the naming of your script caused a circular import for something that doesn’t exist.
You can easily fix this by renaming your script into something else.
I have come across a strange python module import issue.
When I trying to import the boilerpipe module,
from boilerpipe.extract import Extractor
I got this exception:
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/boilerpipe-1.2.0-py2.7.egg/boilerpipe/extract/ __init__.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1140, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
I've searched over internet and saying that in Python 2.6 the 'str' module has been renamed to 'string' module. So this looks like some where in code library it didn't import "string" module properly.
Yet the really strange thing is, when I run the python code from home directory and run the same piece of code (either by using python shell or using python pyfile.py), it works fine! No more import error.
So I'm bit confusing. Can anyone give me any hint?
Thanks!
Some other script in sys.path is called "string.py" and is masking the stdlib module.
Double check to make sure that you don't have a file string.py that has been imported.
To debug this, put somewhere:
import sys
raise Exception("string module: %r" %(sys.modules.get("string"), ))
That will tell you what string module was imported (or if it shows None, no string module has been imported yet).
I am new to python.And i am learning the standard library.
Whenever i run the code below , it always raise the AttributeError...
And it seems like there is something wrong with the import command.
Also , i try to run it on the interactive interpreator,and it works just fine.
The sample code
import tempfile
import os
#temp = tempfile.TemporaryFile()
temp = tempfile.mktemp()
print "tempfile","=>",temp
file = open(temp,"w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()),"byte"
file.close()
try:
os.remove(temp)
except OSError:
pass
The error output
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Original exception was:
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
My enviroment
ubuntu12.04
python2.7
Did you name your own file tempfile.py? If so, rename it, delete all your *.pyc files, and try again.
PS: providing the actual text of the error with the traceback would tell us these things.
Trying to access an attribute that does not belong to a class or function in a module raises an AttributeError exception, the attribute might have been deprecated in a later version of the Python interpreter being used. I suggest you check the version of the Python you're running and make sure your dir(module) includes the attribute you're trying to use