I created a virtual environment with Anaconda on Windows 10 and installed the package in the virtual environment, but I could not import it. In my case, I had a problem setting environment variables.
The flow from creating a virtual environment to installing a package and confirming import.
> conda create -n py37 python==3.7
> activate py37
(py37) > conda install xxxx
> python
Python 3.7.7 (default, Apr 15 2020, 05:09:04) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import xxxx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'xxxx'
The following variables were set in the environment variables. It seems that it was set when building the old environment.
PYTHONHOME=C:\Users\【username】\Anaconda3
PYTHONPATH=C:\Users\【username】\Anaconda3\Library\bin
Let's check the environment with these set.
The original usage of these environment variables is that PYTHONPATH
Specify the path that stores your own library. .. If you output the package read target directory as sys.path
, you can see that the added path is added to PYTHONPATH
.
(py37) > python
Python 3.7.7 (default, Apr 15 2020, 05:09:04) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
#output======================================================
# C:\Users\【username】\Documents
# C:\Users\【username】\Anaconda3\Library\bin <--Has been added
# C:\Users\【username】\Anaconda3\envs\py37\python37.zip
# C:\Users\【username】\Anaconda3\DLLs
# C:\Users\【username】\Anaconda3\lib
# C:\Users\【username】\Anaconda3\envs\py37
# C:\Users\【username】\AppData\Roaming\Python\Python37\site-packages
# C:\Users\【username】\Anaconda3
# C:\Users\【username】\Anaconda3\lib\site-packages
# C:\Users\【username】\Anaconda3\lib\site-packages\win32
# C:\Users\【username】\Anaconda3\lib\site-packages\win32\lib
# C:\Users\【username】\Anaconda3\lib\site-packages\Pythonwin
PYTHONHOME
specifies the path to standard Python libraries. If you try deleting this PYTHON HOME
, you can see that the path switches to the virtual environment. Apparently, if PYTHONHOME
is set, the package loading destination does not switch normally when activating the virtual environment.
(py37) > python
Python 3.7.7 (default, Apr 15 2020, 05:09:04) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
#output======================================================
# C:\Users\【username】\Documents
# C:\Users\【username】\Anaconda3\Library\bin
# C:\Users\【username】\anaconda3\envs\py37\python37.zip <-It's a virtual environment
# C:\Users\【username】\anaconda3\envs\py37\DLLs <-It's a virtual environment
# C:\Users\【username】\anaconda3\envs\py37\lib <-It's a virtual environment
# C:\Users\【username】\anaconda3\envs\py37 <-It's a virtual environment
# C:\Users\【username】\AppData\Roaming\Python\Python37\site-packages
# C:\Users\【username】\anaconda3\envs\py37\lib\site-packages <-It's a virtual environment
# C:\Users\【username】\anaconda3\envs\py37\lib\site-packages\win32 <-It's a virtual environment
# C:\Users\【username】\anaconda3\envs\py37\lib\site-packages\win32\lib <-It's a virtual environment
# C:\Users\【username】\anaconda3\envs\py37\lib\site-packages\Pythonwin <-It's a virtual environment
Delete PYTHONHOME
.
It's okay to have PYTHONPATH
, but I didn't need it so I deleted it.
To enable conda
at the command prompt, add the following path to Path
.
C:\Users\【username】\Anaconda3\Scripts
At the command prompt, add the following path to Path
so that you can use the python
command without activating it. However, if you activate the virtual environment, you can use the python
command, and the virtual environment you are using is obvious, so I don't think you need to add this path.
C:\Users\【username】\Anaconda3
C:\Users\【username】\AppData\Local\Microsoft\WindowsApps
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps
In the above way, when I execute the python
command without activating at the command prompt, the DLL path doesn't seem to be recognized and I need to add the following path to Path
.
C:\Users\【username】\Anaconda3\Library\bin
After all, unless you do special development, if you want to use Python with Anaconda, you should add only C: \ Users \ [user name] \ Anaconda3 \ Scripts
to Path
.
Recommended Posts