0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Windows環境下でpythonでvenvをactivateした上でpipでパッケージを入れたときに実行でpy.exeランチャーにてハマったこと

Last updated at Posted at 2023-07-21

Windows環境下でpythonでvenvをactivateした上でpipでパッケージを入れたときに実行でimportにてハマったこと

概要

venv環境下でpip install で入れたパッケージが実行時に見つからずModuleNotFoundErrorとなった。

対応策は py.exe のランチャーを使わず python.exe を使うこと。

環境

  • Windows10
  • Python 3.11.4
  • pyton標準のvenvにてactivateした状態のCMD.EXE上での処理

発生事象

NG パターン1

(venv_dir) C:\Users\foo>py test.py
Traceback (most recent call last):
  File "C:\Users\foo\test.py", line 34, in <module>
    from docopt import docopt
ModuleNotFoundError: No module named 'docopt'

NG パターン2

(venv_dir) C:\Users\foo>test.py
Traceback (most recent call last):
  File "C:\Users\foo\test.py", line 34, in <module>
    from docopt import docopt
ModuleNotFoundError: No module named 'docopt'

OK パターン

(venv_dir) C:\Users\foo>python test.py
Usage:
....

詳細

詳細は下記の通り python.exe py.exe コマンドラインでそれぞれpathの扱いが違うため。

C:\Users\foo>py -0p
 -V:3.11 *        C:\Users\foo\AppData\Local\Programs\Python\Python311\python.exe
 

C:\Users\foo>py -m env venv_dir
C:\Users\foo>venv_dir\Scripts\activate.bat
(venv_dir) C:\Users\foo>py -0p
  *               C:\Users\foo\venv_dir\Scripts\python.exe
 -V:3.11       C:\Users\foo\AppData\Local\Programs\Python\Python311\python.exe

(venv_dir) C:\Users\foo>py --version
Python 3.11.4

(venv_dir) C:\Users\foo>python --version
Python 3.11.4
(venv_dir) C:\Users\foo>type showpath.py
#! /usr/bin/python
# -*- coding: utf-8 -*-

import sys
import pprint

pprint.pprint(sys.path)

(venv_dir) C:\Users\foo>python -c "import sys;import pprint; pprint.pprint(sys.path)"
['',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311',
 'C:\\Users\\foo\\venv_dir',
 'C:\\Users\\foo\\venv_dir\\Lib\\site-packages']

(venv_dir) C:\Users\foo>py -c "import sys;import pprint; pprint.pprint(sys.path)"
['',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311',
 'C:\\Users\\foo\\venv_dir',
 'C:\\Users\\foo\\venv_dir\\Lib\\site-packages']

(venv_dir) C:\Users\foo>python showpath.py
['',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311',
 'C:\\Users\\foo\\venv_dir',
 'C:\\Users\\foo\\venv_dir\\Lib\\site-packages']


(venv_dir) C:\Users\foo>py showpath.py
['C:\\Users\\foo',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311',
 'C:\\Users\\foo\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages']

(venv_dir) C:\Users\foo>py -m pip list
Package            Version
------------------ --------
docopt             0.6.2
pip                23.2
setuptools         68.0.0

(venv_dir) C:\Users\foo\>py -m pip list -v
Package            Version  Location                                                            Installer
------------------ -------- ------------------------------------------------------------------- ---------
docopt             0.6.2    C:\Users\foo\Documents\venv_dir\Lib\site-packages pip
pip                23.2     C:\Users\foo\Documents\venv_dir\Lib\site-packages pip
setuptools         68.0.0   C:\Users\foo\Documents\venv_dir\Lib\site-packages pip

(venv_dir) C:\Users\foo>python -m pip list -v
Package            Version  Location                                                            Installer
------------------ -------- ------------------------------------------------------------------- ---------
docopt             0.6.2    C:\Users\foo\Documents\venv_dir\Lib\site-packages pip
pip                23.2     C:\Users\foo\Documents\venv_dir\Lib\site-packages pip
setuptools         68.0.0   C:\Users\foo\Documents\venv_dir\Lib\site-packages pip

関連ドキュメント

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?