Python import statement only recognizes packages from certain directories? - python

This is one of the weirder issues I've come across and I can't figure out what is causing it.
I have a from x import x statement and it only works in certain file directories.
for example, from my userroot directory
C:\Users\admin>where python
C:\scoop\shims\python.exe
C:\Users\admin>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit
(AM D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb import InfluxDBClient
>>> ^Z
I can import the package just fine - however when I go to the desktop (which is still apart of my user directory)
C:\Users\admin>cd Desktop
C:\Users\admin\Desktop>where python
C:\scoop\shims\python.exe
C:\Users\admin\Desktop>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AM D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb import InfluxDBClient
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\scoop\apps\python27\current\lib\site-packages\influxdb\__init__.py",
line 9, in <module>
from .client import InfluxDBClient
File "C:\scoop\apps\python27\current\lib\site-packages\influxdb\client.py", li
ne 14, in <module>
import requests
File "C:\scoop\apps\python27\current\lib\site-packages\requests\__init__.py",
line 43, in <module>
import urllib3
File "C:\scoop\apps\python27\current\lib\site-packages\urllib3\__init__.py", l
ine 8, in <module>
from .connectionpool import (
File "C:\scoop\apps\python27\current\lib\site-packages\urllib3\connectionpool.
py", line 3, in <module>
import logging
File "logging.py", line 4, in <module>
from influxdb import InfluxDBClient
ImportError: cannot import name InfluxDBClient
>>> ^Z
So going into my desktop directory, though still using the same python.exe, will not allow my to import the same package.
What makes this more bizarre, if I look at the sys.path
C:\Users\admin\Desktop>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\scoop\\apps\\python27\\current\\python27.zip', 'C:\\scoop\\apps\\pytho
n27\\current\\DLLs', 'C:\\scoop\\apps\\python27\\current\\lib', 'C:\\scoop\\apps
\\python27\\current\\lib\\plat-win', 'C:\\scoop\\apps\\python27\\current\\lib\\l
ib-tk', 'C:\\scoop\\apps\\python27\\current', 'C:\\scoop\\apps\\python27\\curren
t\\lib\\site-packages']
>>> ^Z
And for the root directory -
C:\Users\admin\Desktop>cd..
C:\Users\admin>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\scoop\\apps\\python27\\current\\python27.zip', 'C:\\scoop\\apps\\pytho
n27\\current\\DLLs', 'C:\\scoop\\apps\\python27\\current\\lib', 'C:\\scoop\\apps
\\python27\\current\\lib\\plat-win', 'C:\\scoop\\apps\\python27\\current\\lib\\l
ib-tk', 'C:\\scoop\\apps\\python27\\current', 'C:\\scoop\\apps\\python27\\curren
t\\lib\\site-packages']
>>> ^Z
You can see they are exactly the same. I figured it might be an Environmental variable issue, but why would it work in the userroot and not on the useroot/Desktop? And this doesnt seem to be just a influxdb error, BeautifulSoup does the same thing.
This is being ran from an elevated cmd prompt on an admin account. I've tried uninstalling and reinstalling python. I've tried both python2 and python3. I've tried setting PYTHONPATH.
Any thoughts?

I am late to reply, but I guess I should for others who face the same problem. I was also facing the same problem with
from influxdb import InfluxDBClient
Then I tried
pip install influxdb-client
And then
from influxdb_client import InfluxDBClient
It worked for me. Hope this helps.

Related

Dash - ImportError: DLL load failed: %1 is not a valid Win32 application

I'm currently facing the below error when trying to import dash
import dash
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\dash\__init__.py", line 1, in <module>
from .dash import Dash, no_update # noqa: F401
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\dash\dash.py", line 20, in <module>
from flask_compress import Compress
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_compress\__init__.py", line 1, in <module>
from .flask_compress import Compress
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_compress\flask_compress.py", line 14, in <module>
import brotli
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\brotli\__init__.py", line 3, in <module>
from .brotli import (
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\brotli\brotli.py", line 5, in <module>
from ._brotli import ffi, lib
ImportError: DLL load failed: %1 is not a valid Win32 application.
I already tried uninstalling/installing brotli packages. However, after some digging I assume that the error is possible due to the fact that I have two different python versions on my laptop. I want the 3.7 one to be the main one. The 3.6 version has been pre-installed by my company and can't be edited/removed. When installing 3.7, I had to use the 32x version as I was experiencing errors with the 64x version. Any suggestion how I can resolve the issue?
C:\Users\>python
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
C:\Users\AppData\Local\Continuum\anaconda3>python
Python 3.7.0 (default, Aug 14 2018, 19:12:50) [MSC v.1900 32 bit (Intel)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Error finding the python modules/libraries from the /home/ubuntu location

I have been trying on setup PIL module on my Ubuntu System and it has been giving a lots of error. Finally , i am getting issue as:
when i try to run python from /home/ubuntu, it gives me error as:
ubuntu#ip-<server>:~$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "PIL/Image.py", line 90, in <module>
from . import _imaging as core
ImportError: cannot import name _imaging
but if I run the code from /usr/local, it does not return any error:
ubuntu#ip-<server>:~$ cd /usr/local/
ubuntu#ip-<server>:/usr/local$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>>
there might be an issue with the path or the permission of the required libraries. But i am not sure how to fix this.
I am executing my script from Jenkins and it throws error over there. Please guide how to fix.

ModuleNotFoundError: No module named '' . in the same folder

I have two python files(qq.py and ww.py) are in the same folder.
code in qq.py is:
print('123x')
code in ww.py is:
import qq
when I execute ww.py in CMD, the error was appered:
Traceback (most recent call last):
File "ww.py", line 1, in <module>
import qq ModuleNotFoundError: No module named 'qq'
I don't know if there are some configuration errors.
It would be great If someone can help me work through this issue.
This is my python version.
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
enter image description here
I have changed the code in ww.py
import os
import sys
sys.path.append(os.getcwd())
import qq
now, ww.py can run well

Python 3 cannot import socket in CMD but available in IDLE

I have installed python 3.7 on my computer. It seems not possible to import socket in CMD:
C:\Users\Sina\py
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python37-32\lib\socket.py", line 49, in <module>
import _socket
ImportError: Module use of python27.dll conflicts with this version of
Python.
>>>
But surprisingly, I am able to import socket in IDLE (Python 3.7).
I searched but didn't find any 'python27.dll' file in the installation directory
of python 3.7 (C:\Python37-32)
EDIT: There was a _socket.pyd file at "C:\Users\Sina" which used to make the confliction. I deleted it and the problem got fixed. Thanks to Aran-Fey!

no module named matplotlib on windows

Facing this issue right now, saw the question:
no module named pylab on windows
I have annaconda and python 2.7 installed. I tried in the python shell:
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named matplotlib
and received the following result. I've checked my PATH variable, and I'm not sure how to resolve.
Thanks in advance.

Categories