Vim ALE cannot import python modules from pipenv - python

I have setup a virtual environment with Pipenv like so:
$ pipenv install Flask
then I have activated the virtual environment like so:
$ pipenv shell
And then I run my Flask app with:
$ flask run
This works, the Flask server spins up and the page is served successfully.
Then in another terminal I activate the virtual environment again and then launch vim and edit my flask app file.
That's when ALE kicks in and runs Black and returns Cannot find implementation or library stub for module named "flask". Which I understand is because flask cannot be imported from the virtual environment (if I install flask outside the virtual environment then the error disappears).
I have added let g:ale_python_auto_pipenv = 1 as suggested in a similar question (1). But that does not seem to have an effect.
I have also tried to install Black inside the virtual environment as suggested elsewhere, but it does not seem to do anything.
I have read elsewhere (2) that some vim plugins solve that but could not find which to install.
I have also read (2) that the PYTHONPATH environment variable must be set. I have tried to set it to the Pipenv virtual environment to no avail.
What should I do to make vim and ALE aware of the python packages installed in my current virtual environment made with Pipenv?
(as an aside it looks like YouCompleteMe does not complete or go to definitions of those packages as well, but I understand it could be a different solution and thus a different question.)
(1) False import errors running Pylint and MyPy from vim ALE in pipenv. -- :!pylint % works -- :!mypy % works
(2) https://github.com/dense-analysis/ale/issues/1998

Related

How do I ensure python project in VSCODE is using virutal envirnoment and not defaulting to global?

Having an issue when using flask and flask-wtf. The issue is that for some reason vscode is defaulting to global rather than the newly created virtual environment. In vscode I will click open folder, create the folder and open it in vscode. Then I'll write my code out and then open terminal in vscode and write: python -m venv my_venv, then on the lower left-hand side I will select it as my virtual environment. I then re-open the terminal, write: pip -V and the pip it is directed to is the folder directory with my_venv in the path name.
Then I pip install flask and flask-wtf, check that they are installed with pip list. Everything looks good until I run and vscode says: ModuleNotFoundError: No module named 'flask-wtf'. But when I go to my global environment and where flask is installed too it runs fine. Something is happening to where the python code is recognizing the original flask and not the one in my virtual environment. How do I ensure that the version of flask is the one in the virtual environment and not others installed on my machine.
Thanks
If you are in your terminal in vscode, it should append your command line with your virtual environment name.
So, usually it would show
PS \path
But if your environment is running, it will show
(venv name) PS \path
If it is now showing, be sure to reactivate your virtual environment from your project directory

Flask runs out of virtual environment

I was working in virtual environment but I removed the virtual environment folder in purpose. But I still can run flask restful application as below, while new empty virtual environment(there are no libraries in site packages but even application works like old virtual environment) is active.
flask run
I thought it should have been failed and complaining like:
no module named flask
, because I removed the old proper virtual environment and installed new one then activated it.
And here is the point, when I try the run the main script as below
python3 app.py
It complains as expected:
no module named flask
It seems that when I run with flask run it is not using virtual environment even virtual environment is active. Because of this reason I can't test my application with other python versions and can't test if requirements are installed and run in new other virtual environments. I couldn't understand this behavior of the flask for any help I appreciate.
Thanks in advance.
You may have Flask globally installed.
pip list | grep flask
You can remove that:
pip uninstall flask
or
Create virtualenv with --no-site-packages option:
virtualenv --no-site-packages venv
source venv/bin/activate
Well I figured out what is going on there.
Flask creates a directory named local in home and moved system's interpreter and dependencies there included by flask application that I wrote.
Then it deploys the application from there when run with flask run. So that means my flask app was not running with virtual environment as we guess.
So in that case I recreated my virtual environment and after installing dependencies I run with python3 app.py. Now it seems that it uses the virtual environment dependencies and interpreter.

Virtual environment can't find module that is installed outside the virtual environment

I am trying to set up a virtual environment on my machine, and I am really having trouble.
EDIT: I set up my virtual environment by installing virtualenvironment1.9 and running:
python virtualenv.py -p myVE
Then I add the line
alias goVE='source ~/virtualenv-1.9/myVE/bin/activate'
to my bashrc, and I use goVE to activate the virtual environment. I get a (myVE) at the beginning of the commandline in terminal, so I think it's working. It's just losing the gtk module somehow.
END EDIT
Basically, if I try to
import gtk
in python, it tells me there is no module named gtk. But outside my virtual environment, gtk imports with no problem. I decided it was a problem with the path, so I added
export VIRTUALENV_EXTRA_SEARCH_DIR=$PYTHONPATH
export VIRTUALENV_EXTRA_SEARCH_DIR=/usr/lib64/python2.6/site-packages/gtk-2.0
to my bashrc. Since evidently the gtk module was found in the pythonpath, I thought the first line would do it. When that was insufficient, I tried adding a path specifically to the directory where gtk was located, which I verified by trying
import gtk
gtk.__file__
Since this still didn't work, I tried modifying the virtualenvironment/bin/activate file, so:
PATH = ="$VIRTUAL_ENV/bin:$PATH:/usr/lib64/python2.6/site-packages/gtk-2.0/"
Again, this should be included in the original PATH (unless I'm mistaken), but I'm getting desperate so I tried adding it explicitly to the PATH. For completeness, my PYTHONPATH is set by:
export PYTHONPATH=${PYTHONPATH}:/data/monroe/ebexcode/trunk/GetData/:/home/user1/geach/:/usr/lib64/python2.6/site-packages/
You haven't explained how you created your virtual environment so there's no way to tell if that succeeded. If your virtual environment was created successfully, there's no need to manually munge the paths.
Assuming you have a virtual environment, you haven't demonstrated that you were able to activate it. In order to activate it, you have to run a script in the virtual environment's bin directory. Example, if your virtual environment is in ~/myvenv and you run bash, activate your virtual environment by issuing this at your prompt:
source ~/myvenv/bin/activate
At this point you can use this instance of your shell to install packages into your virtual environment:
pip install PyGTK
Depending on your system installation, this might fail if you do not have all the tools and libraries installed to build the Python GTK bindings.

Python can't find my Flask script under virtualenv

I'm trying to build a simple API server using Flask that will validate and store international phone numbers using this phonenumbers.py. I previously installed Flask and several Flask extensions in a virtualenv, and started building my app. So far so good. But after downloading and installing phone numbers.py using its own installer, I found that python running inside virtualenv could no longer find my app script! Looking at the directory, it's sitting right there, but python does not recognize it any more. The only other wrinkle I can think of is that after doing the install, I used an external text editor (outside the venv) to edit my app script and re-save it.
What have I done wrong?
I compared my environment variables inside and outside virtualenv. They are the same except for the following 3 additions:
VIRTUAL_ENV=/Users/tokrum/callcenter/venv
PATH=/Users/tokrum/callcenter/venv/bin # (was added to the beginning of my $PATH, but the rest of pre-existing PATH is as beforeā€¦.)
PS1=(venv)\h:\W \u\$
My app is called callcenter-v0-1.py. It lives in a directory called /callcenter/, along with the phone numbers-7.0.1 and venv folders at the same level of my directory structure.
Thanks for any light you can shed on this.
Install Flask_script in your virtual env using
$pip install Flask-Script
Make sure that you activated the virtualenv before you installed Flask and your other dependencies -
$ virtualenv env
$ source env/bin/activate
$ pip install flask
Then when you're done working, make sure to deactivate the environment -
$ deactivate
Finally, when you want to start working again, navigate to your project directory and reactivate the environment -
$ source env/bin/activate
At this point, I would just remove the virtualenv and start over.
Hope that helps!

How to setup PyDev's python interpreter in eclipse using pip within a virtualenv

I want to use pip to install new packages and they say to install pip in a virtualenv. I did that and installed some packages and use them no problem.
But I have a problem setting up the simple google app engine helloworld app under PyDev using python2.7. I set it up to use C:\Python27\env\Scripts\python.exe. But running it gave an error that it couldn't import pdb.
Running the virtualenv python from the cmd line and calling 'import pdb' gave no errors. And typing pdb echoed: module 'pdb' from 'C:\Python27\lib\pdb.py'. So from the cmd line, it could find pdb in the root lib dir. The debug out from appengine said it was using the virtualenv python. I made sure that I had C:\Python27\lib in the System PYTHONPATH when setting up the PyDev interpreter, but this did not matter. Strange. (I also have C:\Python27\DLLs and C:\Python27\lib\lib-tk).
So I tried using the root python exe as the interpreter and it works fine. No problem with pdb, so my code is correct. But now it can't find the modules I added using pip in the virtualenv.
It seems there are 3 options for setting this up correctly:
Somehow setup the PyDev python interpreter using the virtualenv python that will find the root pdb package. (Don't know how to do this, but it seems like the right answer.)
Or will it work to use the root python and add the virtualenv site-packages to my path. (not sure if this will work).
Don't listen to the pip advice - install pip in the root env and install my modules in the root env.
I hope someone who has gone through this before can give some advice as to the best way to proceed with the least amount of problems later.
I did some research on the google-appengine-python group and they say to go with option 3: Don't listen to the pip advice when using it with google appengine. appengine does not play nice with virtualenv.
Refs:
Appengine with virtualenv
Strange imports (and WARNINGs) in development server

Categories