[PYTHON] Unable to import packages installed in virtual environment with Anaconda on Windows 10

Introduction

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.

Event reproduction procedure

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'

Cause

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

Solution

Delete PYTHONHOME.

It's okay to have PYTHONPATH, but I didn't need it so I deleted it.

Bonus Other environment variables

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.

In addition, When adding in Windows 10, the windows store will be launched by the python command, so it is necessary to set the priority to the top.

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

in conclusion

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

Unable to import packages installed in virtual environment with Anaconda on Windows 10
Double-click ipynb in windows + anaconda environment to open with jupyter-notebook
Steps to create a Python virtual environment with VS Code on Windows
From Python environment construction to virtual environment construction with anaconda
Create a virtual environment with Anaconda installed via Pyenv
Notes on creating a virtual environment with Anaconda Navigator
Seaborn, matplotlib garbled characters resolved in Windows10, Anaconda virtual environment
How to use VS Code in venv environment on windows
Update packages already installed with pip in user environment (non-su)
A note on how to load a virtual environment in PyCharm
Install Jupiter Notebook on Windows with pip in proxy environment
How to build Anaconda virtual environment used in Azure Machine Learning and link with Jupyter
Various Anaconda settings in Windows 10 environment
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
Change Python 64bit environment to 32bit environment with Anaconda
Python virtual environment and packages on Ubuntu
[5 steps] Apply anaconda virtual environment to VScode
[Python] Create a virtual environment with Anaconda
Creating a python virtual environment on Windows
Build Python environment with Anaconda on Mac
Creating a virtual environment in an Anaconda environment
Get a list of packages installed in your current environment with python
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I installed the library with Visual Studio Code, but Unable to import
Run PIFuHD in Windows + Anaconda + Git Bash environment
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Start Django in a virtual environment with Pipenv
Python (Windows 10) Virtual Environment / Package with VS Code
Create a virtual environment with conda in Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
[0] TensorFlow-GPU environment construction built with Anaconda on Ubuntu
[Memo] Build a virtual environment with Pyenv + anaconda
[Node-RED] Execute Python on Anaconda virtual environment from Node-RED [Anaconda] [Python]
Work in a virtual environment with Python virtualenv.
Flow of creating a virtual environment with Anaconda
Use jupyter-lab installed in python virtual environment (venv)
Use Python in Anaconda environment with VS Code
[Python] How to install OpenCV on Anaconda [Windows]
Error running Jupyter Notebook in Anaconda virtual environment
Create a Python virtual development environment on Windows
How to install / verify graphviz on anaconda / windows10
Use anaconda virtual environment with Zsh (problem fix)
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
I tried to graph the packages installed in Python
Create an Anaconda virtual environment in your project folder
Procedure to install TensorFlow in fish shell environment (Anaconda 4.0.0)
Create a comfortable Python 3 (Anaconda) development environment on windows
How to export / import Zope zope object installed with Plone-4.1.5-UnifiedInstaller-20120604.tgz
Python development environment with Windows + Anaconda3 + Visual Studio Code
Double-click to open ipynb file (Mac, Anaconda virtual environment)
How to resolve SSL module errors in Anaconda environment
Create an OpenAI Gym environment with bash on Windows 10
How to use jupyter lab in Windows 10 local environment
Command line collection for using virtual environment in Anaconda
Until you install TensorFlow-GPU with pip in Windows environment
Ssh to virtual environment with remote development of vscode
Force luigi to do parallel processing in windows environment
How to deal with Executing transaction: failed in Anaconda
How to operate Firefox with selenium on Windows Memo
Make anaconda environment available from command prompt on windows