Error while trying to import socket? - python

I've recently installed python, ipython and pip, with pip I've installed the socket library, and plenty others, now I can't import socket while running python program through cmd but I can do it using ipython,
When I run ipython and I import socket everything works fine,
now I wrote a simple script, called "tcp.py" and all it contains is nothing but the following line
Import Socket
Traceback (most recent call last):
File "C:\dir\desktop\tcp.py", line 1, in <module>
import socket
File "C:\dir\desktop\socket.py", line 1, in <module>
socket
NameError: name 'socket' is not defined
Any idea how to solve this?

You've made two mistakes: the first is to have your own file named "socket.py". This shadows the stdlib module, so when you import socket, it finds your file instead of the module in the stdlib. The second mistake is that your "socket.py" file has the word socket in it. This is what is actually causing your error: the word socket is undefined.
Delete your "socket.py" file, and any *.pyc files lying around, and the problem will be fixed.
If you read the traceback (error report) closely, you can see what's happened.

Related

Error on calling a function from another file on python

I'm getting an error on importing a function from another file in python.
This is my file structure
In checkSpellings.py there's a function defined as spellchecker(tokenized_text) . And I'm trying to use it by importing it on main.py by following code
from checkSpellings import spellChecker
But it gives me an warning (red underline on top of both of checkSpellings(file name) and spellChecker(function name) in import code). This previously happened and I though this is purely an issue with intellisense . Because last time same thing happened (gave me the warning), but the code worked fine.
But now when I run the main.py it gives me an error saying
Traceback (most recent call last):
File "/home/pankaja/Documents/PycharmProjects/BookDigitizer/OCR_Correction/main.py", line 1, in <module>
from checkSpellings import spellChecker
ImportError: cannot import name 'spellChecker
How can I fix this ? What have I done wrong ?
IMPORTANT : Here I'm using python3.6 interpreter on an anaconda virtual environment. Might it be an issue ?
The function is spellchecker but you tried to import spellChecker.

Errors importing pyodbc

My background is Java/C++ and I'm very new to python. I'm trying to import pyodbc to my project which works alright if I do it via command line.
import odbc
However, when I try to do the same in pydev/eclipse, i get the following error which I cannot find a solution to. I suspect this may have something to do with Eclipse setup
Traceback (most recent call last):
File "C:\Users\a\workspace\TestPyProject\src\helloworld.py", line 2, in <module>
import pyodbc
File "C:\Users\a\AppData\Local\Continuum\Anaconda3\Lib\site-packages\sqlalchemy\dialects\mssql\pyodbc.py", line 105, in <module>
from .base import MSExecutionContext, MSDialect, VARBINARY
ImportError: attempted relative import with no known parent package
I'm really stuck here and any hints will be appreciated!
An incorrect library was imported into the External Libraries list of the project. After fixing it, the import is working.

'undefined symbol' error when importing C++ module

I have a python script which runs another python script and this latter script imports a module. It fails to do so and returns the following:
Traceback (most recent call last):
File "/some/path/script.py", line 13, in
import Autodock as AD
File "/some/path/to/module/Autodock.py", line 30, in
import BALL
File "/usr/lib/pymodules/python2.7/BALL.py", line 1, in
from BALLCore import *
ImportError: /usr/lib/pymodules/python2.7/BALLCore.so: undefined symbol: _ZN4BALL25FragmentDistanceCollectorclERNS_9CompositeE
However it gets loaded successfully when I just open the python interpreter and enter this:
>>> from BALLCore import *
Other scripts which also run /some/path/to/module/Autodock.py (which is the script importing the module) run successfully. What makes them successfully import the module from the same module-path?
I need to excuse myself for not sharing so much code, because I have no clue where to look. Any guidance would be appreciated.

ImportError: cannot import name SerialPort on Twisted

I'm having trouble with a simple twisted program and when run I get the following error:
Traceback (most recent call last):
File "serial.py", line 1, in <module>
from twisted.internet.serialport import SerialPort
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/serialport.py", line 22, in <module>
import serial
File "/Users/me/Desktop/serial.py", line 1, in <module>
from twisted.internet.serialport import SerialPort
ImportError: cannot import name SerialPort
I did have a bunch of fancy code, but I determined that the following line is the problem:
from twisted.internet.serialport import SerialPort
I'm running Twisted 13.2.0, Python 2.7 both updated as well as PySerial 2.7 installed. So I believe everything is installed and up to date. This error occurs on two different systems.
The following runs without issue:
python -c "from twisted.internet.serialport import SerialPort"
Thanks for your help. Is it some conflict with PySerial?
Look at these lines of the traceback:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/serialport.py", line 22, in <module>
import serial
File "/Users/me/Desktop/serial.py", line 1, in <module>
from twisted.internet.serialport import SerialPort
Here you see that .../twisted/internet/serialport.py is doing this:
import serial
This is a line from Twisted. You can guess based on this fact that the serial module the code wants to import is not something that you have written.
Then look at the next file in the traceback: /Users/me/Desktop/serial.py. This tells you that Python found the serial module in your personal desktop directory. import serial is now running code you wrote. Based on the previous surmise, this is likely a bad thing.
Get rid of serial.py (and serial.pyc and serial.pyo if they exist) and try again. It still may not work but the cause of the problem (and therefore the traceback) should at least be different.

python email script doesn't work on mac os x 10.9

I started trying to make a script to send emails using python, but nothing worked. I eventually got to the point where I just started copying and pasting email scripts and filling in my info. Still nothing worked. So i eventually just got rid of everything except this:
#!/usr/bin/python
import smtplib
This still did not work. Can someone explain to me why this doesn't work? I'm sure its really simple. I'm using mac os x 10.9 if that makes a difference. here is my error:
Traceback (most recent call last):
File "the_email.py", line 2, in <module>
import smtplib
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
ImportError: No module named utils
Change the name of your script from email.py to something else. It is interfering with the Python standard library module of the same name, email.
Read this: Syntax: python smtplib not working in script
A user says that you have to remove email.py from the folder.

Categories