customtkinter is not recognised by VS Code - python

I am in my early steps to learning python GUI.
I am using customtkinter library.
I have installed the customtkinter on my system.
I created a virtual environment within my project folder
with:
python -m venv 'myvenv'
and activated the env with:
source <path to activate file within 'myvenv' >
suprisingly the customtkinter module still isn't recognized when I import it to my file,
despite being installed (I did pip list on both local and virtual env).
I'm using macOS 12 and python 3.10.6.
help, Thanks.

I think this was caused by the incorrect Python interpreter.
You can use shortcuts "Ctrl+Shift+P" and type "Python: Select Interpreter" to choose your virtual environment.

Related

Is Activate.ps1 required to add packages in venv? vscode

New to utilizing virtual environments. I understand to concept but not the complete execution within vscode.
I have read the vsCode guide on using Python venvs and the vscode guide on getting started with python
I'm not understanding the link between how the Terminal window displays its in the venv and how vscode shows its using the selected virtual interpreter. I want to make sure I know which one I am working in so that I can effetely manage packages.
These two images demonstrate my point. They both show me that vscode is using the .venv interpreter based on the blue region pointed by the red arrow. But the first image shows the Terminal is in the venv while the second image shows the Terminal is in the venv. This is confusing to me because when I use "pip" to install via the terminal I want to make sure its downloading the package to the right venv and not to global. I would assume if vscode is using the venv interpreter the Terminal should also default to the venv.
Do you have to just run the .\venv\Scripts\Activate.ps1 in the Terminal anytime you want to run "pip" to add a new package in your venv?
When the virtual environment name is displayed in front of your terminal directory, it means that your current terminal is using the activated virtual environment. At this time, directly using pip command will use the pip.exe in the virtual environment, and the installed package will also be installed in the virtual environment.
If the virtual environment is not activated, if your current terminal does not display the name of the virtual environment, directly using the pip command may use the pip.exe in the global environment, that is, the python version configured by the computer environment variable, and the package will also be installed under it.
If you're worried, you can use the full install command. For example, the following command uses the complete python interpreter path to ensure that the sklearn package is installed under the virtual environment .venv under folder py12.
e:\workspace\py12\.venv\Scripts\python.exe -m pip install sklearn
PS: If you choose a virtual environment interpreter, then creat a new terminal will automatically activate the environment. This is controlled by the setting below, which defaults to true.
"python.terminal.activateEnvironment": true,

PyCharm doesn't recognize packages installed via Terminal when environment is used in PyCharm in Pop_OS Linux

I'm using Pop_OS! Linux. Whenever I make a Python Virtual Environment using mkvirtualenv and then pip install packages into it, that environment does not work when I reference it inside pycharm. I set up virtual environments using virtualenv and virtualenvwrapper.
The workaround I've found is to build any virtual environment you need to use inside of PyCharm inside of pycharm, or using the terminal inside of PyCharm. I'm annoyed it doesn't seem compatible with my native terminal, and can't figure out why. I even get slightly different python versions by running python --version both inside the PyCharm terminal and in the native linux terminal, while inside the same virtual environment! It's like Pycharm isn't reading the same information out of the python environment directory as my terminal is.
What is going on here, and how do I harmonize PyCharm with the rest of my linux machine? I'm also new to Pop_OS! Trying it out for the first time is an adventure.
IDK if that will work, but maybe if you install your packages via the "Python packages" tab in PyCharm (down left -> add package next to the research bar that appeared), PyCharm will see them while it installed them itself.

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

Module not found error in VS code despite the fact that I installed it

I'm trying to debug some python code using VS code. I'm getting the following error about a module that I am sure is installed.
Exception has occurred: ModuleNotFoundError
No module named 'SimpleITK'
File "C:\Users\Mido\Desktop\ProstateX-project\src\01-preprocessing\03_resample_nifti.py", line 8, in <module>
import SimpleITK as sitk
I installed the module using
sudo pip install SimpleITK
I know that it was installed because I was getting a similar error when I ran the code through the command line, and it was fixed by doing the above. I don't understand why VS code does not recognize that
After installing a new module via pip reloading vscode may work if vscode doesn't recognize it. To do this, make sure that the module is installed inside the virtual environment by creating and activating a virtualenv:
python3 -m venv env
source env/bin/activate
Make sure to use the correct way of installing a module with pip:
python3 -m pip install {new_module}
Replace the string "{new_module}" with your module name. After that, make sure to reload vscode by clicking Ctrl+Shift+P, and selecting Reload window.
Now vscode will know the new module and autocompletion works.
sudo pip install is most likely installing globally into a Python interpreter that is different than the one that you have selected in VS Code. Please select the Python interpreter you want to use and then install explicitly using that interpreter (if you're not using a virtual environment then use something like /path/to/python -m pip install SimpleITK, although I strongly recommend using a virtual environment and to not install packages globally).
In Mac, correctly selecting the Python Interpreter worked for me:
From within VS Code, select a Python 3 interpreter by opening the Command Palette (⇧⌘P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
No interpreter selected
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, see Configuring Python environments.
Source :VS Code Select Interpreter
This error: your vscode use other python version. This solution change vscode use current python.
In terminal find current python version:
py --version
In vscode Press Ctrl+Shift+P then type:
Python: Select Interpreter
Select current python version
I ran into this problem with VSCode and resolved it by setting my Python interpreter within VSCode to the same as the one in my system path (type "echo %PATH%" on Windows and look for Python) via the process here: https://code.visualstudio.com/docs/python/python-tutorial#_select-a-python-interpreter
There are a lot of proposed answers that suggest changing the launch.json or the settings.json file. However, neither of these solutions worked for me.
My situation:
Is Python environment selected? yes
Does the Terminal recognize Python environment? yes
Can I run the Python code from the activated Terminal? yes
Does the code run w/o error when I use "Start Debugging"? yes
Does the code run when I click "Run Code"? no
The only solution that worked for me is to:
Open Windows Terminal (or cmd)
Activate environment: conda activate <environment_name>
Open Visual Studio Code from Terminal: code
Then, "Run Code" (#5) works without any issues.
Source:
"module not found error" in VS Code using Conda - l3d00m's answer
Faced similar issue and here is how I fixed it. Remember that there are multiple ways to run your code in VS code. And for each way you may end up with different interpreters and environments. For example:
1. Creating virtual env and installing libraries
In my case I opted into creating virtual environment and doing so outside of VS Code using command prompt:
python -m venv .plotting_test
Following that I activated it:
.plotting_test\Scripts\activate.bat
Following that I installed additional libraries:
python -m pip install matplotlib
Following that I made sure to see it was all installed ok:
python -m pip list
And I also checked where for current directory:
cd
2. Point VS Code & VS Code Code Runner to virtual environment
Opened vs code, closed previous workspaces, opened new folder, created test.py as I was starting new. Pressed ctrl + shift + p. Selected ```Python: Select Interpreter``:
Followed by + Enter interpreted path
Navigated to directory from last step from section 1. Found my virtual environment folder created in step one and pointed VS code to that version's python.exe in Scripts:
Verified I am pointed to such:
Saved as workspace so that I can create default workspace settings for this project:
In workspace settings files defined paths to my virtual environment created n step 1 for workspace interpreter & CODE RUNNER(!):
"settings": {
"python.defaultInterpreterPath": "C:/Users/yyguy/.plotting_test/Scripts/python.exe",
"code-runner.executorMap": {"python": "call C:/Users/yyguy/.plotting_test/Scripts/activate.bat && python -u"}
}
}
Reloaded window just to make sure (ctrl + shift + p) = "Developer: Reload Window"
Now run code and run python file should be execute under your specified envs:
Try running pip list in VS Code to check if the module is installed, next check if your python version is correct/supports that version of SimpleITK. It may be a problem with the python interpreter that you are using for VS Code (ie. the module may be installed on a different python instance than the one your VS Code is using)
Is Python environment selected?
Does the Terminal recognize the Python environment?
Can I run the Python code from the activated Terminal?
Does the code run w/o error when I use "Start Debugging"?
if the answer to the above is "yes."
Then,
Try running the Code using the option "Run python file in terminal" (in code runner extension). And assign a new shortcut for that for future use...
How to fix module not found error in Visual Studio code?
To Solve VSCode ModuleNotFoundError: No module named X Error Make sure you are running from the package folder (not from package/module ) if you want import module. calculations to work. You can also set the PYTHONPATH environment variable to the path to the package folder.
Once you have created a virtual environment, and installed your required packages in that environment, close VS code. For Windows platform, open command prompt and navigate to the folder where your virtual env folder is created. And then launch VS code from there using the command code .
For ex: My virtual env name is .imgenv, and its inside C:\py_stuff\projects
So, I navigate to C:\py_stuff\projects and then type code .
Now, your VS code should recognize the packages !
I just ran into the same issue. I found that if I selected all text before shift enter the script would compile as a file instead of as a single line.
I had the same problem. I bet you have a shebang statement at the top of your file.
If you do.
Visual Studios settings
Under "Code-runner->Code-runner: Respect Shebang" section or just do a search for "Code-runner: Respect Shebang"
Uncheck weather to respect Shebang to run code.
Now it will run under the virtual environment and find the modules that you installed using pip! :)
I struggled with this for a very long time, and had tried almost every other answer. I wasn't using pip, so that wasn't the issue. But still VS Code wasn't finding the modules that were installed in the Selected Interpreter.
Ultimately it came down to old conflicts that existed because I switched to miniconda, and VS Code was still looking for anaconda3.
I completely wiped VS Code and its associated files (cache, preference files, etc.) from my machine (some instructions), and installed a clean version.
This now syncs as expected with miniconda.
If you have different python versions installed, be sure you install module with right one.
python -m pip install <module>
or
python3 -m pip install <module>
Run your environment from a directory not in the users directory. I solved my problem running my environment from C:\Code\ProjectA\
I discovered my problem by running:
IMPORT os
Mycwd = os.getcwd()
PRINT(Mycwd)
.venv/Lib/SitePackages is the default directory where Vscode looks for Modules.
This directory is automatically created on creating .venv via the command Pallete.
External modules installed via pip are placed in this directory by default.
Place self created modules inside this folder manually.
For mac users
In the terminal check which python you are using by command which python. It will give you the path of the python interpreter path. After that type cmd shift P and type Python: Select interpreter.
After that select + Enter interpreter path and paste the path which you got after running the command which python.

No module named 'numpy' in pycharm

I am quite new to python and pycharm.
I installed python 3.6 through anaconda, and I can see many packages including numpy are installed as I can see them in cmd (im using windows) by typing 'conda list'. Also, it works if i type 'import numpy' in python through window command prompt.
However, if I open pycharm and run "import numpy" there, it gives me 'No module named 'numpy' in pycharm'. May I know whats wrong with my setting? I guess it must be some problem with my interpreter setting.
I think my python is installed in C:\Users\AAA\Anaconda3\python.exe
I checked in pycharm, project interpreter is "C:\Users\AAA\PycharmProjects\untitled\venv\Scripts\python.exe"
Should I change it to the one under anaconda3 folder?
What is venv folder under "pycharmprejcts"? Is it a virtual environment? It shows (see the attached screenshot) the base interpreter is the one under anaconda? Should I choose to inherit global site-packages?
You should select Conda environment in Pycharm, not create a new, blank Virtualenv
Or at the very least - System interpreter, then find the Python executable for Anaconda
What is venv folder under "pycharmprejcts"? Is it a virtual environment?
Yes, it allows your project to be more portable - you define the minimum set of dependencies for your code rather than rely on everything installed only locally on your machine
You have a virtual environment, this helps keep the dependencies separate between projects. You can set your project interpreter to C:\Users\AAA\Anaconda3\python.exe or install the dependency in your venv, in pycharm you would press alt+enter on the import and press install.

Categories