I've encountered one peculiar issue I just can't understand.
I've been working on a code in PyCharm, using Python 3.6 (Windows 10) and I've imported few modules for that objective like so:
import requests
from bs4 import BeautifulSoup
import time
import lxml
For few days now I've been coding having no problems, I've run my code several times to test it and everything been going well until suddenly, without changing anything in the interpreter or any other setting, the modules seem to generate a lot of import errors such as:
ImportError: cannot import name 'BeautifulSoup'
AttributeError: module 'requests' has no attribute 'get'
This is so weird, suddenly I can't run my code and all my work is halted.
I've been looking up the internet and saw that usually this happens if you call one of your files in a same name as a module (for example, naming my file bs4.py), I'm afraid this is not the case here, I made sure my file is named in a unique name which doesn't exist anywhere on said libraries.
I couldn't find any other solution for this.
Can anyone help me figure this one out?
Related
Code that worked for months suddenly stopped working.
In a file in the same directory 'downloaders', I have an import 'import image_downloader'. Somehow, the import doesn´t work anymore. The file that imports it is in the same directory.
My PyCharm IDE shows no problems at all, no warning or anything. But when I run it, the ModuleNotFoundError happens:
ModuleNotFoundError: No module named 'image_downloader'.
I have looked at similar questions here, but they haven´t helped me so far. I am desperate, and would be thankful for tips on resolving this annoying issue.
I found the solution. Not sure why it doesn´t work anymore with the old import style.
But what made it work: using absolute imports. This means in this case, instead of import image_downloader, I used from downloaders import image_downloader, even when the importing file is also in the package image_downloader.
I don't have a lot of programming experience, so please try to keep answers relatively noob-friendly! :) Basically, I have a Python...library, I guess? or module? that I need to run in Spyder through Anaconda. This seems to be a very obscure module called PySPM, used for analyzing scanning probe microscopy data, that I received from a colleague my lab collaborates with. When I try to run a piece of software which uses this module, it gives this error:
ImportError: No module named 'PySPM.io'
The code itself which triggers this reads as follows:
from os import path
from PySPM.io.Translators.Utils import uiGetFile
from PySPM.io.Translators.BEPSndfTranslator import BEPSndfTranslator
from PySPM.io.Translators.BEodfTranslator import BEodfTranslator
from PySPM.analysis.BESHOFitter import BESHOFitter
The first line that says from PySPM.io.Translators.Utils import uiGetFile is what's triggering the error. I'm really stuck scratching my head here. What's going on and how can I solve it?
Pycroscopy is the (thoroughly) reorganized version of PySPM.
#Martensite try:
$ pip install pycroscopy
Still learning certain things about Python... I am having issues recognizing my Python script in my scripts dir. First, I checked to see that my path is set correctly:
import sys
for pythonPath in sys.path:
print pythonPath
And C:/Users/..../Documents/maya/2014-x64/scripts is listed, which is where I am placing swap.py
In Maya's script editor I am typing the following:
import swap
reload(swap)
swap.printSomething()
I get:
Error: AttributeError: file line 3: 'module' object has no attribute 'printSomething' #
If I take the same code and throw it into a package...
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
And then call this, it works...
import swapPackage.swap as swap
reload(swap)
swap.printSomething()
Why? I am totally confused. Mel scripts even run fine from this location as well. I just can't get a simple python script to import and run.
Also something I noticed. Even though I can get this script to run in a package, the package name must be totally different than the module name. I can't have a package named this:
C:/Users/..../Documents/maya/2014-x64/scripts/swap/swap.py
but I can have one where the package name is different:
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
Ok folks, I was able to solve this by executing a print of my file, only to find out that it was sourcing a totally different version someone copied elsewhere. ARGH. This solves both issues, and makes sense why changing the package name from the module worked.
import swap
reload(swap)
print swap.__file__
The following is my code.
import http
h1 = http.client.HTTPConnection('www.bing.com')
I think it's ok.But python give me the following error:
AttributeError: 'module' object has no attribute 'client'.
I wanted to know why and how to fix it.Thanks.
First, importing a package doesn't automatically import all of its submodules.*
So try this:
import http.client
If that doesn't work, then most likely you've got a file named http.py, or a directory named http, somewhere else on your sys.path (most likely the current directory). You can check that pretty easily:
import http
http.__file__
That should give some directory like /usr/lib/python3.3/http/__init__.py or /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/__init__.py or something else that looks obviously system-y and stdlib-y; if you instead get /home/me/src/myproject/http.py, this is your problem. Fix it by renaming your module so it doesn't have the same name as a stdlib module you want to use.
If that's not the problem, then you may have a broken Python installation, or two Python installations that are confusing each other. The most common cause of this is that installing your second Python edited your PYTHONPATH environment variable, but your first Python is still the one that gets run when you just type python.
* But sometimes it does. It depends on the module. And sometimes you can't tell whether something is a package with non-module members (like http), or a module with submodules (os). Fortunately, it doesn't matter; it's always save to import os.path or import http.client, whether it's necessary or not.
I have some code in a module called XMLModel.py that parses an XML object with lxml.etree. When I try importing lxml.etree generically in an IPython or regular Python shell, it works fine. Command line versions of my code all work fine.
But when I try to have Apache execute the code as part of a web page, I get a bizarre import error:
File "/var/www/html/../ws/python-util/src/util/XMLModel.py", line 4, in <module>
import lxml.etree
ImportError: /opt/epd/7.3-2/lib/libxslt.so.1: undefined symbol: xmlXPathCompiledEvalToBoolean
I've searched for this "undefined symbol" problem but cannot make any sense about it. It might have something to do with building a static instead of dynamic version of lxml but I cannot find anything that spells it out clearly.
Has anyone else had this kind of problem specifically within a browser setting? What could make the import suddenly fail when the code is used that way?
My operating system is Red Hat 4.1.2-48. The directory /opt/epd is just where I store the Enthought Python distribution and then also place related modules, .so stuff, etc. It's all very standard.