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 6 years ago.
I have a problem, when try to use requests lib
import requests
r = requests.get('http://www.python.org/')
print(r)
After that, I got the following error
Traceback (most recent call last):
File "C:/Users/admin/Documents/alex/test.py", line 3, in <module>
import requests
File "C:\Program Files\python3\lib\site-packages\requests\__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "C:\Program Files\python3\lib\site-packages\requests\packages\__init__.py", line 27, in <module>
from . import urllib3
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 35, in <module>
from .connection import (
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\connection.py", line 44, in <module>
from .util.ssl_ import (
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\util\__init__.py", line 20, in <module>
from .retry import Retry
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\util\retry.py", line 15, in <module>
log = logging.getLogger(__name__)
AttributeError: module 'logging' has no attribute 'getLogger'
I do not understand why is it at all. Please, help me.
Your problem is one of the following
Your installation of Python is terribly broken
When C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\util\retry.py tries to import logging it imports wrong file. This may happen because
There is file called logging.py in the directory you are running your test.py from. In this case, you will need to rename it so that logging from Python library is imported, not yours.
There is file called logging.py in one of directories from Python Path and it gets found before the logging.py the module actually needs
To check what logging gets imported, write the following simple program
import logging
import os.path
print os.path.abspath(logging.__file__)
Whatever is printed is path to your logging file. If it is not along the line of ...\Python\\Python36\\lib\\logging\\__init__.py, the wrong file is imported and you got to replace/rename it
Related
I am using Splunk 8+ and Python 3.6. I am trying to run this https://github.com/0x616c6578/TA-geoip2 plugin.
I get this error message when I look into search.log and try to run the Python command in the log by itself:
C:\Users\strozllc>"C:\Program Files\Splunk\bin\Python3.exe" "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\geoip-command.py
Traceback (most recent call last):
File "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\geoip-command.py", line 8, in <module>
from splunklib.searchcommands import \
File "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\..\lib\splunklib\searchcommands\__init__.py", line 145, in <module>
from .environment import *
File "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\..\lib\splunklib\searchcommands\environment.py", line 20, in <module>
from logging.config import fileConfig
File "C:\Program Files\Splunk\Python-3.7\lib\logging\config.py", line 30, in <module>
import logging.handlers
File "C:\Program Files\Splunk\Python-3.7\lib\logging\handlers.py", line 26, in <module>
import logging, socket, os, pickle, struct, time, re
File "C:\Program Files\Splunk\Python-3.7\lib\socket.py", line 49, in <module>
import _socket
ImportError: Module use of python27.dll conflicts with this version of Python
n.
Huh? I looked at line 8 in geocommand.py and it is from
splunklib.searchcommands import \
dispatch, StreamingCommand, Configuration, Option, validators
I look it C:\Program Files\Splunk\bin, and there is indeed a python27.dll. But how is it getting called?
This is a case of Python 3 trying to run Python 2 code. The latest versions of Splunk (you didn't say which one you're using) only support Python 3 so an error will be thrown when an outdated library file is encountered.
I'd suggest filing an issue on GitHub, but the app appears to be abandonware since it hasn't been touched in a year despite having 4 issues.
Consider forking the code and updating it yourself.
I'm trying to get the Revit python shell debugger working in my dynamo but I'm running into issues. It seems to run in Revit alright but I cant get the debugger in dynamo to work as shown here.
Also if anyone has any other methods for debugging python code in Revit, feel free to let me know!
Here is the error I'm getting:
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File "<string>", line 13, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\__init__.py", line 36, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\db\__init__.py", line 19, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\db\view.py", line 10, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\db\collector.py", line 35, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\ui\__init__.py", line 1, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\ui\forms\__init__.py", line 12, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\ui\forms\flexform.py", line 6, in <module>
File "C:\Users\cmaltez\AppData\Roaming\Dynamo\Dynamo Revit\2.6\packages\RevitPythonWrapper\extra\rpw.zip\rpw\ui\forms\resources.py", line 41, in <module>
File "C:\Program Files\Autodesk\Revit 2021\AddIns\DynamoForRevit\IronPython.StdLib.2.7.9\wpf.py", line 11, in <module>
ImportError: No module named _wpf
Which is odd because it seems I have the package located in the directory from the install.
This is what my Dynamo looks like. its the getting started file that comes with RPW.
Here is what I've tried so far:
I've added this path to my searching path here: "C:\Program Files (x86)\IronPython 2.7\Platforms\Net40" according to this post.
Here is an image of my searching path:
RPS works in Revit but doesn't work in dynamo for debugging python scripts.
Here are the contents of wpf.py
#-*- coding: ISO-8859-1 -*-
def _():
import sys
if sys.platform == 'cli':
import clr
clr.AddReference('IronPython.Wpf')
_()
del _
from _wpf import *
any and all help would be appreciated!
So after hours of testing different things, I finally tried to just open the wpf.py file and change the line:
from _wpf import *
to
from wpf import *
and I'm now able to access rpw in dynamo.
but I cant get the debugger working in pycharm still.
I also added the paths to the Net40 folder, the unzipped rpw folder that comes in the install, and the location of my iron python folder
UPDATE: if I change the name of the select module (in which the import error occurs) to select1, the problem goes away! WHAT the?
First off: everything works fine when I try to import my package from the command line, or run pytest in the project directory.
But in PyCharm I'm getting the "attempted relative import beyond top-level package" problem when I try to run my tests, and ONLY while in PyCharm. I can't figure out why.
I have even deleted and re-cloned the repository and the problem still occurs.
Here is the link to the problem branch.
And here is the tb:
Testing started at 4:02 PM ...
C:\Users\ricky\AppData\Local\Programs\Python\Python37\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/ricky/PycharmProjects/candejar/tests
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pycharm\_jb_pytest_runner.py", line 10, in <module>
from pkg_resources import iter_entry_points
File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\site-packages\pkg_resources\__init__.py", line 36, in <module>
import email.parser
File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\feedparser.py", line 27, in <module>
from email._policybase import compat32
File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\_policybase.py", line 9, in <module>
from email.utils import _has_surrogates
File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\utils.py", line 29, in <module>
import socket
File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\socket.py", line 52, in <module>
import os, sys, io, selectors
File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\selectors.py", line 12, in <module>
import select
File "C:\Users\ricky\PycharmProjects\candejar\candejar\select.py", line 24, in <module>
from . import exc
ImportError: attempted relative import with no known parent package
Process finished with exit code 1
I'm sure there is something simple going on here but I just can't figure it out. NOTE: I'm using Python 3.7 RC 1.
The sys.path in pycharm and the command line come out to be the same, except pycharm also includes the pycharm project main package directory (which for some reason appears twice).
I was able to solve this by deleting the .idea directory from the cloned repository and re-opening PyCharm. I still don't know what was wrong, but it is fixed now. Very weird.
UPDATE: it has something to do with the following line in the project .iml file in the .idea folder:
<sourceFolder url="file://$MODULE_DIR$/candejar" isTestSource="false" />
Removing that xml code solves the issue. But I do not know why, and I don't know how it got there in the first place (I do not recall messing around with any of my settings prior to this error).
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm 3.0.1\helpers\pydev\pydevd.py", line 2, in <module>
from django_debug import DjangoLineBreakpoint
File "D:\Program Files\JetBrains\PyCharm 3.0.1\helpers\pydev\django_debug.py", line 3, in <module>
from pydevd_comm import CMD_SET_BREAK
File "D:\Program Files\JetBrains\PyCharm 3.0.1\helpers\pydev\pydevd_comm.py", line 107, in <module>
import pydevconsole
File "D:\Program Files\JetBrains\PyCharm 3.0.1\helpers\pydev\pydevconsole.py", line 6, in <module>
from code import compile_command
ImportError: cannot import name compile_command
My Code is the simple webpy example,there is no problem with it。
Most likely you've created a module named code.py.
If you do this, it hides the stdlib module code, so anything that needs to use that module will find your module instead.
And the reason you get this error is that your code module doesn't have anything named compile_command.
In general, you always want to avoid giving your modules the same name as anything in the stdlib, or any site-packages you've installed. You can work around this by, e.g., organizing your modules into packages instead of leaving them all at the top level, but it's easier to just not re-use the names.
I have a very wierd problem.
When i run the file from a different directory it runs fine.
but other wise i get this error
from textprocessor import *
File "/home/mohit/Documents/analysis/categorization/textprocessor.py", line 2, in <module>
import nltk
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 101, in <module>
import ccg
File "/usr/local/lib/python2.7/dist-packages/nltk/ccg/__init__.py", line 15, in <module>
from chart import *
File "/usr/local/lib/python2.7/dist-packages/nltk/ccg/chart.py", line 33, in <module>
from nltk.parse.api import *
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/__init__.py", line 46, in <module>
from featurechart import *
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/featurechart.py", line 24, in <module>
import nltk.data
File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 41, in <module>
import zipfile
File "/usr/lib/python2.7/zipfile.py", line 460, in <module>
class ZipExtFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'
I am tryin to import nltk module
You've called a script in the same directory "io.py", and this is interfering with the io module in the stdlib. Rename it.
You must have a script in your folder named nltk.py, io.py, zipfile.py, or some such. When you run the file in the same directory, it is imported instead of the appropriate python or nltk module.
Where is nltk? Is it pure Python code? You may want to put that in site-wide modules folder or in your cwd.