In my workplace we manage multiple python flask projects and all of them are configured in the VSCode workspace. Each of the projects are separate folders and each of them has .venv folder created under it. We had a windows system earlier with which i'm able to run an application in VSCode (which starts with the venv activate automatically), and when we stop that application and run another application (different project with different venv) it automatically changes the application folder path, exiting previous venv and activating the current one.
Recently we shifted to MAC and this particulat auto switching to different venv is not happening or not sure whether its happening. It stays in the same venv (as per $VIRTUAL_ENV and oh-my-posh prompt which always shows the current virtual env). But the status bar in the vscode shows the different venv like Project: Flask (venv)
Can someone help me to find which config needs to be done to get this properly. VSCode Launcher.json / settings.json / venv path in VSCode settings.
pipenv --where shows the venv in current path, not the one which is activ
pip -V shows current venv
$VIRTUAL_ENV var is also showing current venv
VSCode status bar debug configuration shows the app which ran last
Issue 1: How exactly we find out which venv is active (googling provides so much of options and all of the results differs with each one).
Issue 2: How do i make sure the respective venv is activated when the app is executed. (please dont tell me to manually exit and activate each time before running app).
Thanks a lot for your efforts if you are reading till now.
Related
My Flask App server is running but I have three imports that cannot be resolved.
I have tried:
reinstalling the imports individually
reinstalling requirements.txt
I configured VSCode Workspace with an extra path to my project folder (there is now a .vscode file within the root of my project folder where it was not before)
I have updated my venv path settings in VSCode
Here is my file structure:
- > .vscode
- > client *(React front end)*
- > data
- > server *(Python/Flask back end)*
- > app
- > venv
- config.py
- README.md
- requirements.txt *(this contains the 3 unresolved, along with several that are resolving)*
- .env
- .flaskenv
- .gitignore
- requirements.txt
Unfortunately none of these things have resolved my imports issue and my routes are still not working. Any ideas/suggestions?
Open the Command Palette (Ctrl+Shift+P), then select the Python: Select Interpreter. From the list, select the virtual environment in your project folder that starts with .env.
Run Terminal: Create New Integrated Terminal (Ctrl+Shift+` or from the Command Palette), which creates a terminal and automatically activates the virtual environment by running its activation script.
Install sqlalchemy and mongoengine with command pip install. Once installing them successfully, there will intellisense when you import them and no warnings shown.
Besides, the folder .vscode is to store Workspace settings as well as debugging and task configurations.
If you are using a virtual environment, and even after trying pip installing all the necessary libraries, you have to select the python interpreter that exists in the virtual environment folder.
(Ctrl+Shift+P) then search for "Python: Select Interpreter"
Click "Enter interpreter path" followed by "Find.."
Navigate to your project virtual environment folder
Go into "Scripts" folder and then select "python.exe" as the interpreter.
These steps allow you to select the right python interpreter associated with the project's virtual environment.
Open the Command Palette (Ctrl+Shift+P), then select Python: Clear Cache and Reload window.
Voila, all the import errors vanished.
I specified a path to the python interpreter I'm using within the settings.json file contained in the project repo's .vscode folder.
"python.pythonPath": "path-to-interpreter.python.exe"
Thanks to the following resource! https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b
I ran into this error after an upgrade of my local python version (brew -> manual install), even though the specified interpreter was already /usr/local/bin/python3.
CMD + Shift + P and re-selecting the [same] interpreter fixed the error.
In hindsight, I suspect that a restart of VSCode could have also fixed this. 🤷♂️
Perhaps VSCode is using the incorrect Python path for this reason. A base interpreter should be used instead of the vscode interpreter, if necessary.
After verifying my Python interpreter was sourced correctly in VS Code, I simply cleaned my env and reinstalled the project locally and the import errors disappeared. I personally had an odd VS Code/Pylance cache and reinstalling the dependencies and modules fixed it for me.
I was getting the very same error you have and none of the solutions presented here worked me.
I work on a windows machine. I use miniconda to manage my virtual environments. And when I'm coding I launch every program from the command prompt (cmd), including Vscode.
Even tough inside Vscode the python evironment was correctly settled I was getting the very same import error that you mention. The interesting part of it was that I wasn't having any problems to run the code, it was working as usuall. But when coding I wasn't able to see the functions related to the libraries with the import error.
The solution:
Activate the correct conda env for the project before launch Vscode from the command prompt.
Why does this happen?
I BELIVE that this happen because when you don't activate any conda env before launch Vscode. The base conda env is loaded as default. That generates the import conflict. (You can chek this out installing those packagues into the environment and running everything just as you have been doing)
This error suddenly came up when I created a new Django project. I used pip to install all the packages in the virtual environment.
I had exactly the same problem and there are several solutions out there, but only #Neha's answer has brought me to the point. The problem here is really simple. VS Code uses your "main" Python interpreter, whereas you should use the one in your virtual environment for Django.
In other words, I have my main Python installation here:
c:\users\yourName\appdata\local\programs\python\python39.
And my VS Code has pointed to exactly this interpreter. But my Django was installed in this path:
c:\users\yourName\source\vscode-repos\basic-app\backend-service\venvs\lib\site-packages.
In order to solve it click on the
VS Code bar with Python Interpreter, you will then see a pop-up window. Choose + Enter interpreter path... then Find.... It will open File Explorer for you. Go to your virtual environment folder, where you have your Django installed (in my case it the vscode-repos\basic-app\backend-service\venvs folder), inside of it go to the Scripts folder and pick python.exe, like this. And you are good to go!
Actually all of the answers were right , but what worked for me is:
Type pip show Django in vsc terminal
Go to the path of intallation mentioned there
It will be inside "lib" by default..go back to scripts
Inside the scripts , there will be python .exe app
Choose this as your interpreter (as per above answers)
Select the Python interpreter in which you have created the virtual environment. I had the same error and this solution worked.
Below is a screenshot that you can refer to.
If you are using VScode, click the python version on the bottom left corner and select the venv where you have installed the packages.
For anyone who can't see the virtual environment then click on the python version below right corner of vscode,click on Enter Interpretor Path,click find,chose the folder that you are working,click on virtual environment name ,then click bin and select python version and the warning will be gone.
In my case (on MacOS) the problem was caused by creating venv using default arguments.
I don't have Scripts directory, but I've got bin inside. The python3 -m venv my_venv command was creating symlinks to python and python3 executables which was confusing for the VSCode. After removing and creating again the venv with below command everything started to work
python3 -m venv --upgrade-deps --copies my_venv
Go to the Python(Extentions) below your screen Like this then select interpreter tab will pop up, then select C:\Python39\python.exe Like this.Hope it helps
What worked for me was to install django within my virtualenv. All other settings were correct in Visual Studio Code.
I am working on a commit-msg git hook. Everything works except importing of specific modules. The bad behaviour is specific to the use of the vs-code GUI git bar (little check mark at the top of the source control panel) with pip installed modules. If I use git commit -m "some message" through the terminal, everything works perfectly.
But, when I use the vs-code GUI, I get an import error.
Thinking it may be an environment issue, I ensured that the module needed was available globally by pip installing the needed module to my machine outside of any virtual environment. It didn't help.
To see what's going on, I wiped my hook and replaced it with nothing more than help("modules") to see if my modules were indeed there. They were not. So I ran
import os
dir_path = os.path.dirname(os.path.abspath(__file__))
print(dir_path)
This printed my path to the file being ran and was the path expected.
I opened a new terminal outside of vs-code with no virtual environment enabled and ran python3 path/to/file to see if my module would show up there. It did.
Additionally, it is there if I run it in the vs-code terminal as well. The only place it seems to be missing is wherever the vs-code git GUI pulls it's package list from.
Where does vs-code git get it python package list and how can I ensure that either; 1) it points to my normal python package list or 2) I can add the needed modules into it's path?
Thanks
EDIT: Thanks to the intelligent suggestion of John Szakmeister left in the comments, I have configured my hook to call env. I then initiated the hook through the terminal and through the GUI to compare.
Using the GUI adds a few variables, mostly for logging purposes and error handling. But there are a couple that I don't quite know what they do. APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=true and
ELECTRON_RUN_AS_NODE=1
However, being that it works in terminal but not GUI, I have to assume that the added variables are not the culprit. So, instead I look at what is different from one to the other.
Outside of the additional VS-Code specific variables, there really isn't very much that is coming up different.
Shell level is 2 through terminal but only 1 through the GUI (interesting...)
Terminal has extra variables TERM=xterm-256color and TERM_PROGRAM_VERSION=1.46.0 that aren't present in GUI env.
Additionally, there are some colors that are set in terminal but not the GUI.
Lastly, there is one variable that is in both that is different. GUI gives _=/usr/share/code/code while terminal yields _=/usr/bin/git. I assume that these refer to what program initiated the terminal but I haven't been able to verify that.
That's about it. PATH is the same for both, as is SHELL, as are just about all of the GIT_anything variables (outside of the fact that the GUI sets a pager that the terminal does not)
I have a script (personal, not for distribution) that works on one of my computers. I want edit it on another computer.
On the first computer, the script was created without a virtual environment. I want to start using them on the second computer.
I have these set up on the second computer.
Windows 10
VS Code 1.45.1
Python 3.8.3
Paths:
Python - C:\Python38
Virtual environments - C:\Users\<User>\Envs
Projects folders - C:\Users\<User>\Documents\python-projects
Environmental variable WORKON_HOME is set to virtual environments path
I copied the script to the project folder. After activating the venv with workon, I pip installed the external modules on the second computer.
I can see the modules when the virtual environment is activated and not when it is deactivated. I believe this means the virtual environment is working and the modules were properly installed.
However, I keep getting the ModuleNotFound error for the external modules. I've tried uninstalling and reinstalling the venv and the modules but I keep getting the error. To ensure it's not the computer, I deactivated the venv and installed the modules and the script worked. I even tried creating a new script with only import requests and I still get ModuleNotFound.
What do I need to do to get the script to use the modules in the virtual environment?
You can do this multple ways. Some are listed here.
1.using IDLE
>>> help("modules")
2.using PIP
$ pip list
3.Using Anaconda
$ conda list
Before doing this, activate particular environment.
I want to debug an application using Python and Flask in VSCode. I have installed Flask and the app runs perfectly fine through cmd. But, when I try to debug it through VSCode, it gives the following error:
cd 'c:\Users\Aditi\CleanHandymanApp';
${env:FLASK_APP}='NewApp'; ${env:PYTHONIOENCODING}='UTF-8';
${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\Aditi\envs\CleanHandymanApp\Scripts\python.exe'
'c:\Users\Aditi\.vscode\extensions\ms-python.python-2018.10.1\pythonFiles\experimental\ptvsd_launcher.py' '--client' '--host'
'localhost' '--port' '63143' '-m' 'flask' 'run' '--no-debugger' '--no-reload'
No module named flask
Can you please help me.
This error message can occur if you installed the python3 version of flask but Visual Studio Code tries to run your project with python2.
Make sure to select the correct version of python in the editor. This can be done by running the command Python: Select Interpreter from the Command Palette (Ctrl+Shift+P).
Activate your virtualenv and run
pip3 install -r requirements.txt
to reinstall all packages inside the venv.
For some reason VS Code thought I was missing all my packages the first time I debugged even though the app was running fine locally.
Sometimes you can get this error if you loaded Flask into a folder which has sub-files. For instance, if you loaded flask into the parent folder with the virtual shell instance but you're running your code in the child file (let's say parent is called crypto_files and inside that is a python source code file called blockchain.py ), then in order to get flask to run properly you'd have to run the file like this:
python crypto_files/blockchain.py
This allows your machine to see Flask running inside crypto_files but also run blockchain.py .
OR, it's possibly you could just reload Flask into the sub(child)file... blockchain.py and then you'd run it from within the subfile.
This complication is mainly due to modern "virtual instances" and shells which are basically like creating a virtual computer-machine inside your ACTUAL hard machine. Flask does this to avoid running everywhere, and since Flask is modular it allows each of your projects to run different modular configurations of Flask to suit each project precisely. The alternative would be awful: you'd have to load the fattest version of Flask with dozens of add-ons for each project, and so all your git and all your projects would have tons of extra code. Flask is built to be very small at the core to avoid this problem (too verbose!).
If you have installed flask in virtual environment, you should have activated it first.
source /path to env dir/bin/activate #in linux
workon 'name of env' #windows
Another option is add sys.path.append('d:/programas/anaconda3/lib/site-packages') in c:\Users\Aditi.vscode\extensions\ms-python.python-2018.10.1\pythonFiles\experimental\ptvsd_launcher.py
Being that "d:/programas/anaconda3/lib/site-packages" should be modified by your local python packages.
Use this command in the terminal instead of selecting run code:
python3 "insert your file name here without the quotes"
e.g.: python3 example.py
I had a variant of the issue mentioned by #confusius. I had installed both Python 3.9 and Python 3.10. I had added Flask to Python 3.10. I had been using one vscode workspace which had selected Python 3.10. I started another project in a different vscode workspace and it had selected Python 3.9 by default, which I didn't notice because I thought it would select the same Python I had already selected in the other workspace.