2

I've found already similar questions here, but could not find specific solution. I've got virtual environment, which is activated and running, and within it I've installed flask-wtf module, as:

pip install flask-wtf

Then, there is script called "test.py", which contiains from flask_wtf import FlaskForm -> and when it is started from terminal as

python test.py

Error is raised, as

    from flask_wtf import FlaskForm
ModuleNotFoundError: No module named 'flask_wtf'

Command

which flask_wtf

returns nothing. But, I could see flask_wtf folder in flaskEnv/lib/python3.6/site-packages folder (where flaskEnv - is directory for virtual environment).

Following the advise, which I've found here on website, running script as

/home/kosist/Documents/Environments/flaskEnv/bin/python test.py

works just fine - script is executed without error. But - why it happens like this? I don't want to enter all the time full path to python, and moreover, command

which python

returns the following:

/home/kosist/Documents/Environments/flaskEnv/bin/python

so calling python already refers to that path!

Also, script works, if flask-wtf module is installed globally, via

sudo pip install flask-wtf

Then I could run script as

python test.py

and everything works.

Could someone, please, help and explain, what is going on, and how to fix it? Because I'm afraid, that I'm missing some simple key point about calling modules from virtualenv, and similar situation could happen to any of the possible installed modules.

davidism
  • 121,510
  • 29
  • 395
  • 339
kosist
  • 2,868
  • 2
  • 17
  • 30

2 Answers2

1

You can verify which packages are installed by using:

pip freeze

You can also verify that pip is running in a virtual environment by using:

pip -V

This last command will output the path to the current virtual environment, what I would recommend you to do is first verify that you're actually using the virtual environment, install flask-wtf and then verify that flask-wtf was installed using pip freeze.

  • 1
    Thanks a lot! I've check it once more, and really, flask-wtf is installed within virtual environment. But, anyway I can't execute script by running "python test.py", it drops an error that ModuleNotFoundError: No module named 'flask_wtf'. But pip freeze tells, that flask_wtf is installed, withing current active virtual environment... What could be wrong with it, please? – kosist Aug 20 '18 at 19:01
  • While installing using sudo, it does not depend on activated environment, I've checked it. When I have activated virtual environment, and install packages using sudo pip, then package is installed globally - because when I deactivate virtual env, and do pip -list; it shows that one installed package... And when flask-wtf is installed in virtual env. using pip install, then script does not run... – kosist Aug 22 '18 at 18:58
  • My mistake, using sudo does install globally even when used inside a virtualenv [How to install a package inside virtualenv?](https://stackoverflow.com/a/44075783/10247340). Check out this answer to a related question [How to install a package inside virtualenv?](https://stackoverflow.com/a/29846865/10247340), I hope it helps – Kevin Islas Aug 23 '18 at 19:14
1

Finally, the reason was found - I've forgotten, that I had specified alias to python3.6 in ~/.bash_aliases file... So in this case command

which python

returned path to virtual environment python, but calling of

python

called python3.6, which is installed globally... After ~/.bash_aliases file was removed, everything works.

kosist
  • 2,868
  • 2
  • 17
  • 30