简体   繁体   中英

ModuleNotFoundError: No module named 'surprise' and others(I have various version of python)

I am now using python with mysql by mysql-python-connector but there's some problem of module importing.

I import modules like this.

import mysql.connector
import os
import surprise
from surprise import Dataset
from surprise import Reader
from collections import defaultdict
import numpy as np
import pandas as pd
from sklearn import tree
import graphviz
from mlxtend.frequent_patterns import association_rules, apriori

when I import module in the python shell(VScode terminal) like this

Shins-MacBook-Pro:part2 yunnys$ python
Python 3.6.8 (default, Apr 11 2020, 15:36:35) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import surprise
>>> from surprise import Reader
>>>

it works

But when I run my python project

Shins-MacBook-Pro:part2 yunnys$ /usr/local/bin/python3 /Users/yunnys/Desktop/part2/DMA_project2_team00.py
Traceback (most recent call last):
  File "/Users/yunnys/Desktop/part2/DMA_project2_team00.py", line 7, in <module>
    import surprise
ModuleNotFoundError: No module named 'surprise'
Shins-MacBook-Pro:part2 yunnys$

It says it cannot find the module.. What is the problem?

I use pyenv and when I do $pyenv versions I can see this.

  system
* 3.6.8 (set by /Users/yunnys/.pyenv/version)
  3.6.8/envs/seminar
  3.7.7
  3.8.0
  seminar

and I can see the modules have already existed.

Shins-MacBook-Pro:part2 yunnys$ pip3 list
Package                    Version
-------------------------- -------
graphviz                   0.14
joblib                     0.15.1
mysql                      0.0.2
mysql-connector-python     8.0.20
mysql-connector-repackaged 0.3.1
mysqlclient                1.4.6
numpy                      1.18.4
pandas                     1.0.3
pip                        20.1.1
protobuf                   3.12.1
python-dateutil            2.8.1
pytz                       2020.1
scikit-learn               0.23.1
scikit-surprise            1.1.0
scipy                      1.4.1
setuptools                 40.6.2
six                        1.15.0
surprise                   0.1
threadpoolctl              2.0.0

And this is pypath!!

Shins-MacBook-Pro:part2 yunnys$ python
Python 3.6.8 (default, Apr 11 2020, 15:36:35) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Users/yunnys/Desktop/part2', '/home/user/mymodule', '/Users/yunnys/.pyenv/versions/3.6.8/lib/python36.zip', '/Users/yunnys/.pyenv/versions/3.6.8/lib/python3.6', '/Users/yunnys/.pyenv/versions/3.6.8/lib/python3.6/lib-dynload', '/Users/yunnys/.pyenv/versions/3.6.8/lib/python3.6/site-packages']
>>> 

Oh I solve it. I change the python version in the VS code from 3.8.2 to 3.6.8 and it works now? Is VS code use python in a different version from system?!

VS 代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM