Can no longer access debugger in BBEdit virtual environment on MacOS Catalina - python

I upgraded to Catalina today, which required updating BBEdit as well. Now I am having trouble using my virtual environment, specifically the debugger in BBEdit.
I installed python3 in a virtual environment via Homebrew, set the correct path, and can access the virtualenv correctly via:
cd /Users/joecat/venvjc
source /Users/joecat/venvjc/bin/activate
I used pip3 to install modules needed
I am trying to run this test script:
#!/Users/joecat/venvjc/bin/python3
import pandas
print('hi')
The pandas module is only in the virtual environment so it should work. If I "Run in Terminal" through BBEdit, the code executes:
But when I try to run in debugger via Command+D, the code executes without the "pdb" debugger popping up at all:
Then, if I adjust the first line to be a "fake" path (assuming this just skips it) it correctly opens up the debugger, and pandas can't be imported because it's no longer using the virtualenv, which is correct:
#!/Users/joecat/venvdddddjc/bin/python3
import pandas
print('hi')
What am I doing wrong with respect to changes to Catalina/BBEdit? I switched to using zsh over bash... is that part of the problem? I am a newbie and happy to provide more details if needed. Thanks for any help anyone can provide.

There have been a lot of changes over time to how BBEdit interacts with the shell environment. As of 13.0.4, BBEdit will run your login shell to capture its environment, whenever you run a #! script. But there are special cases for Python, Ruby, and Perl, since those all have built-in support in BBEdit.
One thing to be aware of: if you've switched to zsh from bash, a common practice is to copy your bash environment from .bash_profile to .zshrc. This won't work correctly in BBEdit, because the shell that BBEdit runs is not an interactive shell.
For that reason, you should set up the majority (and perhaps all) of your zsh environment by putting the declarations (PATH, etc) in either .zprofile or .zshenv (but not both). Any interactive stuff can remain in .zshrc.
(And if you're still stuck, writing to support#barebones.com is always a good idea.)
(p.s. I would not recommend using any BBEdit version older than 13.0.4 on Catalina.)

I was able to correct the issue by downgrading to BBEdit 12.1.16, the earliest release that works with Catalina, as opposed to the latest version 13.0.2 that I was using.
This may be a bug in BBEdit, or else there's something else that changed this functionality between 12 and 13.
Would love to learn more if anyone has any information on this issue. Google is turning up next to nothing on this problem

In case it is helpful to anyone who comes across this issue, I was able to resolve this by importing and setting trace on the pdb module in lines 2 and 3 (right after the virtual environment path set on line 1):
import pdb
pdb.set_trace()
This allows the debugger to run normally in BBEdit 14.1.2, macOS Monterey 12.3.1.
No idea what the underlying issue is. I guess pdb is naturally imported when using the native OS python, but must be manually imported when using a virtual environment.
Hope this helps.

Related

Import "flask" could not be resolved from source Pylance (reportMissingModuleSource)

When I am writing
from flask import Flask
One Yellow line is coming up under flask and stating Import "flask" could not be resolved from source Pylance (reportMissingModuleSource) . Also, I am able to do the work with this package also successfully. But the thing is, I am not able to use autosuggesstions for Classes and methods very well.
Further:
I have checked that flask is installed successfully.
Also I visited this thread https://github.com/microsoft/pylance-release/issues/236
And I set up my settings.json in vscode as follows:
"python.analysis.extraPaths": [
"/media/sarimurrab/New Volume/COURSES/Flask/FlaskMigrateforDatabaseMigrations/2"
]
But Still, unable to resolve the error.
When I did not install the module "flask" in the Python environment currently used in VSCode:
Please use the command "pip --version" to check the source of the module installation tool "pip", the module is installed at this location:
Then, we can use the command "pip show flask" to check the installation location of the module "flask": (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)
If the "reportMissingModuleSource" message is still displayed here, please reload VS Code.
(F1, Developer: Reload Window)
Are you using a Virtualenv? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.
To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.
I had a similar issue while trying to import flask on vscode.
I fixed it by using anaconda. Simply you install the flask module in your created environment example screenshot.
How to create a virtual env in anaconda:
1. On the left sidebar, click on environments.
2. Click create (at the bottom).
3. At the pop-up window, give your vir.env a name
and select the language version.
4. Once created, you can start installing
different modules in your environment.
I hope that helps!
For Linux Mint and for those who have installed flask, but VSCode doesn't find it:
check the Flask path: pip show flask (should be smth like
Location: /home/<username>/.local/lib/python3.8/site-packages
in VSCode click left bottom button and choose the python interpreter, in my case I changed it from python3.9 to python3.8 as we can see it in the flask path.
That's because you have not chosen your path correctly,
type:
pipenv --venv
then it will show you where your virtual env is installed. Check where the packages are installed in your env, and then type what comes to you from the shell\scripts or whatever\python, and the packages will work.
I solved my problem by using a "Global" version of Python. Maybe Pylance had not updated to work with the version of Python I was using.
interpreter language screenshot
I was facing same issue. I tried all solutions from stack-overflow but none worked. But after lot of searching and time waste I found my silly mistake. I had created folder named 'flask' and stored my project there. I'm beginner and going through lot of such silly mistakes. Hope it would help, if somebody commits same mistake.
I think this is a Vscode problem ,just restart the Vscode .
By the following steps, I solved this issue:
1)On the project directory create the .flaskenv file
In the .flaskenv file write the following two lines:
FLASK_ENV=development
FLASK_APP=main.py
Please pay attention that main.py is my main file after writing flask run in the Vscode terminal, additionally, you have to create an env folder or requirement file proviosly.
A few answers (Jill's, Marius's, and Roy's) mention the fact that is necessary to choose the correct Python interpreter to make Pylance function properly. I would like to add the fact that this is still necessary to do when using a Jupyter Notebook with the correct Python kernel already chosen.
It is counterintuitive to choose both Python interpreter and notebook's Python kernel to make things work. It is even more counterintuitive considering the fact that Python interpreter's button (on the left bottom of the screen, on status bar) does not necessarily appear when a Jupyter Notebook is open, but when a Python script is open. For instance, in this screenshot, we see the little line under Scikit-learn's import, indicating a problem with the import (even though the import is successful). However, the correct Python kernel, with Scikit-learn installed, is already chosen. Only opening a Python script we notice that the Python interpreter is the reason of this behavior, because a wrong one is chosen, without Scikit-learn. In some sense, one could think that the reason behind this was a problem with the Python kernel or the Conda environment (it is common to experience this kind of problem when experimenting with Jupyter Notebook and Jupyter Lab). I hope this answer may help those who are searching for solving this problem in the specific context of Jupyter Notebooks inside VS Code. They could ignore the other answers because they could think it is not the case for them.

Two python3 interpreters on win10 cause misunderstanding

I used win10. When I installed Visual Studio2017, I configure the Python3 environment. And then after half year I installed Anaconda(Python3) in another directory. Now I have two interpreters in different directories.
Now, no matter in what IDE I code the codes, after I save it and double click it in the directory, the Python File is run by the interpreter configured by VS2017.
Why do I know that? I use sys.path to get to know it. But when I use VS2017 to run the code, it shows no mistake. The realistic example is that I pip install requests in cmd, then I import it in a Python File. Only when I double click it, the Traceback says I don't have this module. In other cases it works well.
So, how to change the default python interpreter of the cmd.exe?
Just change the interpreter order of the python in the PATH is enough.
If you want to use python further more, I suggest you to use virtual environment tools like pipenv to control your python interpreters and modules.

Why won't PyCharm see my libraries?

If I do something like "import selenium" (or any other kind of third party library) in a .py file and then run it from the terminal, it works just fine. But if I make a new file in PyCharm CE and do the same thing, it can't find the library / module.
How can I fix this or get it to point in the right location? I use a Macbook Pro.
You need to setup your project in PyCharm to use the Python interpreter that has your libraries:
Go to: file->settings->project->project interpreter
And select the appropriate interpreter from the dropdown. After selecting an interpreter, the window displays a list of libraries installed on that interpreter; this should further help you make the right selection.
I've faced a similar issue on Pop!_OS after installing PyCharm via Flatpak. I think the installation is somehow incomplete, as I've had these issues (among others):
Installer could not create the menu shortcut due to the lack of credentials. Unlike during a typical installation, it wouldn't ask for the password and instead I had to uncheck that option altogether.
Built-in terminal defaulted to sh. Even after changing to bash, it would not read my .bashrc and many commands were missing.
After changing the interpreter into a local virtualenv, it would just default to Python 3.7 (even though the version was actually 3.8) and it didn't see any of my installed libraries.
When I've tried to use a Docker Compose environment, IDE failed to detect Docker Compose installation.
I've eventually uninstalled PyCharm and downloaded it directly from Jetbrains website to make it work correctly.

OSX Anaconda default python

I have anaconda distribution installed on my macbook to my local home directory /Users/simon/anaconda
When I open up iterm and type python I see the default OSX install of python. I saw this thread here and implemented the fix mentioned: Mac using default Python despite Anaconda install
My .bash_profile currently looks like this:
export PATH="$HOME/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH"
A few strange things happen:
When I type python I still see the OSX python despite having changed my .bash_profile
If I source /Users/simon/.bash_profile this actually fixes the problem. Using python now brings up anaconda's python
When I quit iterm or restart my computer and try to load python again, it goes back to the OSX version. .bash_profile remains unchanged.
When I try all of the above using terminal everything works fine. The issues described above appear to be specific to iterm2 and I'm not sure why
How do I get my system to use anacondas python instead of the OSX version?
It seems that your $PATH is redefined somewhere else in your configuration. This usually doesn't cause issue if you know in which order they are loaded and keep track of what's going on.
I recommend a bit of reading to understand more the load order.

VS code runs python with no errors only if started from conda terminal

One of the projects that I work on in Python is pretty big and uses a lot of libraries.
I started developing it in Spyder then switched to VS Code.
I also use conda environment.
The problem is with VS Code only: when I open VS Code itself and try to run the project - I get all kinds of import errors.
However, if I open the conda terminal first and just type "code" in it and execute it - the VS Code opens, I can select my project and everything runs just fine, no errors.
In both cases the environment is the same, I did the Ctrl+Shift+P to select it, plus it shows as selected in the bottom left corner anyway. It's the default env.
How can I fix this so I don't have to start VS Code from conda terminal each time?
at the conda the modules are installed by default.
in vscode or any other of ides you have to install it manually.
open your terminal or cmd and type:
pip install module-name
I found what the problem was so I'll just leave it here in case it may happen to someone else.
My VS Code was using Powershell as the default shell, even though the input was right with the correct conda env selected (the base in my case), even with running
conda activate base
command to ensure it, it would still somehow mess up and complain about missing imports
So the solution was to set the default shell in VSC to regular Windows cmd.exe - that was the only needed change

Categories