Properly using Anaconda's Python as the PATH's "python" - python

I've noticed that while I can point my environment variables to Anaconda's Python interpreter, I don't get a fully working Python environment when I execute python in the command prompt. Python will still launch, but many of its packages are "broken."
PROBLEM:
Anaconda's Python interpreter does launch when called from command prompt, but it isn't able to import some of its packages (numpy being one of them used in this example).
EXAMPLE(WINDOWS CMD):
> where python
C:\Anaconda3\python.exe
Great, python calls the right interpreter. Let's go in there and import numpy!
> python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
from . import multiarray
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.
This is weird. Let's try launching "Anaconda Prompt", which appears to just be an activated Anaconda environment.
>python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
Everything works fine this way.
SUMMARY:
I'm expecting Anaconda's Python interpreter to launch from the command prompt and behave just the way it would if launched from within the Anaconda Prompt (which appears to just be a command prompt with an activated Anaconda environment).
What can I do to be able to get a fully working Anaconda Python interpreter by just launching python from the Window's command prompt? Maybe what I'm really asking is, what do I need to change in my environment variables to ensure that the Anaconda environment is activated when I call on python from the command prompt?

Activating a conda environment can also change the PATH. I suggest you compare PATH before and after activating the environment.

Related

Issues importing pymssql in Python3 while using conda environment

I am having trouble importing a library called pymssql.
I have read previous solutions regarding this issue on stack overflow. The following are the solutions that did not work:
Error importing pymssql
import pymssql Unicode DecodeError in windows 7
I am running a conda virtual environment with Python 3.7.9. The following is the output when I run python in the terminal.
Python 3.7.9 (default, Aug 31 2020, 07:22:35)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
So, this confirmed that I was using the correct Python version. Next, I tried to import a library called pymssql.
This looked like:
>>> import pymssql
This returned an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/__init__.py", line 3, in <module>
from ._pymssql import *
File "src/pymssql/_pymssql.pyx", line 1, in init pymssql._pymssql
ImportError: dlopen(/usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so, 2): Symbol not found: _iconv
Referenced from: /usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so
Expected in: flat namespace
in /usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so
>>>
I have tried conda install pymssql and pip install pymssql. Neither of these solved the issue.

What does the following error mean: AttributeError: module 'importlib._bootstrap_external' has no attribute '_w_long'?

Recently installed a github package through conda in windows that has created a new Python environment.
Whenever I try to run Spyder in the new environment I get the following error:
Error processing line 1 of C:\Users\cip18jjp\Anaconda3\envs\ox\lib\site-packages\matplotlib-3.2.1-py3.8-nspkg.pth:
Traceback (most recent call last):
File "C:\Users\cip18jjp\Anaconda3\\Lib\site.py", line 168, in addpackage
exec(line)
File "<string>", line 1, in <module>
File "C:\Users\cip18jjp\Anaconda3\Lib\importlib\__init__.py", line 51, in <module>
_w_long = _bootstrap_external._w_long
AttributeError: module 'importlib._bootstrap_external' has no attribute '_w_long'
Remainder of file ignored
Python 3.8.2 | packaged by conda-forge | (default, Mar 23 2020, 17:32:17) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
osmnx and the command as given on the github page: conda config --prepend channels conda-forge conda create -n ox --strict-channel-priority osmnx
Solved: Path variable in windows pointed to some different Python file locations.
This is because python and pip are installed on different sources. For more info, please look at the following post
using pip3: module "importlib._bootstrap" has no attribute "SourceFileLoader"

How to get SublimeText to load DLLs where they load fine using commandline

When using python through the commandline, I am able to import modules fine. However, when I build a python script in SublimeText3, some modules don't import. I generally receive the following error:
ImportError: DLL load failed: The specified module could not be found.
The problem looks to involve modules with dependencies on modules with a "_" prefix (_sqlite3, _ssl). For example:
File "C:\ProgramData\anaconda3\Lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "C:\ProgramData\anaconda3\Lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
System:
Windows 10
SublimeText3 (freshly reinstalled during troubleshooting)
Python 3.7.3 [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
(freshly reinstalled during troubleshooting)
Both commandline and ST3 or pointing to same instance of Python.
This is the only python install on the machine.
Have used default Python build in ST3 and created a new build, both have same issue.
Results when using the commandline:
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import plotly
>>>
Results in ST3:
import plotly
Traceback (most recent call last):
File "C:\Users\usr\Desktop\scrap.py", line 1, in <module>
import plotly
File "C:\ProgramData\anaconda3\Lib\site-packages\plotly\__init__.py", line 32, in <module>
from plotly import (
File "C:\ProgramData\anaconda3\Lib\site-packages\plotly\graph_objs\__init__.py", line 3, in <module>
from plotly.basedatatypes import BaseLayoutType as _BaseLayoutType
File "C:\ProgramData\anaconda3\Lib\site-packages\plotly\basedatatypes.py", line 25, in <module>
from .callbacks import (Points, InputDeviceState)
File "C:\ProgramData\anaconda3\Lib\site-packages\plotly\callbacks.py", line 2, in <module>
from plotly.utils import _list_repr_elided
File "C:\ProgramData\anaconda3\Lib\site-packages\plotly\utils.py", line 262, in <module>
from chart_studio.utils import *
File "C:\ProgramData\anaconda3\Lib\site-packages\chart_studio\__init__.py", line 2, in <module>
from chart_studio import (plotly, dashboard_objs, grid_objs, session)
File "C:\ProgramData\anaconda3\Lib\site-packages\chart_studio\plotly\__init__.py", line 10, in <module>
from . plotly import (
File "C:\ProgramData\anaconda3\Lib\site-packages\chart_studio\plotly\plotly.py", line 38, in <module>
from chart_studio.plotly import chunked_requests
File "C:\ProgramData\anaconda3\Lib\site-packages\chart_studio\plotly\chunked_requests\__init__.py", line 1, in <module>
from . chunked_request import Stream
File "C:\ProgramData\anaconda3\Lib\site-packages\chart_studio\plotly\chunked_requests\chunked_request.py", line 4, in <module>
import ssl
File "C:\ProgramData\anaconda3\Lib\ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: DLL load failed: The specified module could not be found.
[Finished in 2.9s]
Any help much appreciated, been stuck on this for days.
For those with a similar issue in the future, I've fixed this by uninstalling the Anaconda distribution of Python and replacing it with a base Python installation from Python.org, then installing packages using pip manually. While not the ideal solution, this has solved the issue so that when I build a Python script in ST3, all modules are imported fine.

Import *.pyd file Cygwin Python

I'm having an issue where my Cygwin Python installation does not recognize *.pyd files. It receives an ImportError: No module named 'understand' when attempting to import the *.pyd file. It is able to detect the file with os.path.exists().
I've experimented with other *.pyd files and have the same No module named XXX issue. I don't think it's a path issue as I tried putting .py files in the folder and was able to call them for other folders. What could I do to troubleshoot this issue?
(Mon Nov-11 4:42:49pm)-(CPU 27.4%:0:Net)-(ssia:/cygdrive/c/Program Files/SciTools/bin/pc-win64/python)-(11M:2)
python3
Python 3.4.5 (default, Oct 10 2016, 14:41:48)
[GCC 5.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
import understand
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'understand'
import os
os.path.exists("understand.pyd")
True
Hm, solved.... In some sense.
Cygwin's python does not allow the import of *.pyd files. I was forced to install a new version of python and use that instead.

"import numpy" results in error in one eclipse workspace, but not in another

Whenever I try importing numpy in my new installation of Eclipse and Pydev, I get the following error:
Traceback (most recent call last):
File "Q:\temp\test.py", line 1, in <module>
import numpy
File "C:\Python26\lib\site-packages\numpy\__init__.py", line 132, in <module>
import add_newdocs
File "C:\Python26\lib\site-packages\numpy\add_newdocs.py", line 9, in <module>
from lib import add_newdoc
File "C:\Python26\lib\site-packages\numpy\lib\__init__.py", line 4, in <module>
from type_check import *
File "C:\Python26\lib\site-packages\numpy\lib\type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "C:\Python26\lib\site-packages\numpy\core\__init__.py", line 13, in <module>
import defchararray as char
File "C:\Python26\lib\site-packages\numpy\core\defchararray.py", line 23, in <module>
from numpy.core.multiarray import _vec_string
ImportError: cannot import name _vec_string
However, in a vanila python console and in IPython, import is successful
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
Q:\Groups\Bioinformatics\HPLCautomatization\autohplc>ipython
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [2]: import numpy
To make stuff even more complicated, in another Eclipse workspace, this import works fine. Restarting eclipse didn't help. I also tried to re-config the interpreter in Preferences->python->interpreter->python (as in this question), but the problem still exists
Two additional similar problems were described on SO (this and this), but both of them were observed with IronPython and I couldn't find anything I could use in the few answers that were provided.
EDIT As suggested by dutt, I checked Preferences->Pydev->Interpreter python->libraries in both the tested workspaces (the working one and the one that doesn't). The malfunctioning workspace contained the following library in system libs C:\Python26\lib\site-packages\numpy-1.5.0-py2.6-win32.egg . On the other hand, the working workspace doesn't list any numpy library in its system libraries list. I have removed the above library from the list in the not working workspace.
(Assuming you'r using PyDev) Look in Preferences->Pydev->Interpreter-Python->Libaries, you may have to add NumPy there. Should be located in your python/lib/site-packages folder.
check the version of your python release and numpy release. they should be the same.
It happened to me and I solved it re-installing my python and numpy for win32, although I have a 64-bit machine.

Categories

Resources