Python VSCode: Installed modules not recognised (closed) - python

Edit: It's working fine now, it seems there was just something weird going on with VSCode
I am writing a discord bot in Python using Gitpod (which has VSCode Browser as its editor). I have installed Pycord 2.0 in the workspace using pip (obviously) but VSCode doesn't seem to be recognising it. I also have the Python VSCode extension installed which adds a 'Run' to the editor when a python file is selected. It used to work just fine but since a month ago it stopped working and the 'Run Code' button is running some sort of Python installation where none of the modules I installed are there. It also doesn't show any code hints for the modules.
I have tried pip show py-cord and changing the selected VSCode python interpreter accordingly. I've also tried changing th interpreter and installing Pycord into that interpreter but none of that worked.
I am running Gitpod on an iPad 6 (model A1893, iPadOS 15.6.1) in Safari
Does anyone know what might be wrong?
Thanks :)
~ R2509

Many developers have multiple python versions installed, and multiple environments with the same python versions, but different libraries installed. Therefore, we need to specify which python compiler VSCode needs to use.
Check on the bottom right of your VSCode, there should be a number down there representing an installation of python.
Upon clicking on it, VSCode will open a window on the top part, allowing you to select which of the many pythons you have installed you want to use. Find the one where you installed your libraries, and you should be good to go.

Method one:
Use pip show Pycord to see where you installed the package Pycord
then choose that environment
Method Two:
Choose a python version you want to use ( Ctrl+Shift+P --> Python:Select Interpreter )
Create a new terminal activation environment
Install the package using pip install pycord in the new terminal
As a suggestion:
you can use a virtual environment to better manage python versions and various packages.
# Create a virtual environment named .venv
python -m venv .venv
# Activate this virtual environment
.venv\scripts\activate

Related

After two VScode updates the function import (cv or numpy) does not work, even selecting others python interpreter path

I often use python in VScode. However, after two VScode updates, the import function (cv or numpy) is not working. I've tried using the shift+command+p and > python select interprete but none are working for these functions.
I tried use > pip install pylint but it didn't work.
How can I solve this problem? Has someone the same problem in VScode?
It is recommended to use a virtual environment to manage third-party packages and python versions more conveniently.
Create a virtual environment with the following command
python -m venv myenv
After the command is completed, select the virtual environment interpreter just created in the Select Interpreter panel
The new terminal can automatically activate the environment
Now there is no package installed in the new virtual environment, and an error occurs
Install the cv2 package in the new environment, and the error disappeared after installing the package
Steps I would suggest to follow:
install the package again (cv2), even if it is installed.
check vs code if it has any updates for the python extension.
update vs code application.
restart vs code.
hope this helps.

Why isn't autocompleteworking in vscode for python external packages?

I am new to python and I guess pyCharm is the most preferred IDE for python but I want to stick to vscode if possible. I have the python extension installed in vscode and autocomplete works fine for default python packages but for external packages like pillow or PyPDF2, its not working.
I created a virtual environment where I installed the above 2 packages using pip and even checked if the packages are installed using pip freeze.
Pip freeze is showing:
Pillow==9.0.1
PyPDF2==1.26.0
Below is my project structure. I need to import PyPDF2 module from basic.py. Also, if it helps, I am using python 3.9.6. Any help or suggestions are greatly appreciated.
You need to select the interpreter by following the docs.
Through the python extension, you can set the interpreter so that the packages will be taken from the environment. The interpreter is basically the python that your virtual env uses so it has access to the python packages that you installed in the virtual env.
Here are the steps in case the docs change:
CTRL+Shift+P or Command+Shift+P depending on if you're on Windows or Mac respectively
Type Python: Select Interpreter after the >
Then you can either select the interpreter from your virtual env that you created or enter the path to the interpreter.
Did you select the interpreter inside the vscode?
Enter vscode and type
ctrl+shift+p
and type in command prompt
>python:Select Interpreter
and then activate the Interpreter.
This will do the trick for you.

Question about pip using Python from Windows Store

I have python installed through windows store and I can install programs using pip, but when I try to run said programs, they fail to execute in powershell.
How can I make sure that the necessary "scripts" folder is in my path? I never faced these problems when installing from executable.
For example, "pip install ntfy" runs successfully in Powershell.
The command "ntfy send test" fails telling me the term is not part of a cmdlet, function, etc. etc.
The 'ntfy' program is located here /mnt/c/Users/vlouvet/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0/LocalCache/local-packages/Python37/Scripts/ntfy.exe
What is the recommended way of editing my path so that programs installed via pip are available across windows store updates of the Python language?
In advance
I highly recommend you not to use python installed from the Windows Store, because you'll face such errors, and even more nasty ones.
The easy solution
Create a virtual environment on a more accessible folder, for example in C:\Users\<user>\python. To do so, do the following:
Using PowerShell, go to your user folder, using cd(Note that usually PowerShell already starts inside your user folder. This is an important setting to have, and if not, you should change your powershell starting point to this folder for the future.);
Now that you're in your user folder, type in the PowerShell mkdir python; cd python;
Now, to create a virtual environment, type python -m venv venv;
(You can verify that your virtual enviroment has been created by listing the folders, with the command ls);
You have created a virtual environment. Now, you need to activate it. To activate, run the following: ./venv/Scripts/activate;
Now, you have fully created and activated a virtual environment for your current PowerShell session. You can now install any packages / programs using pip.
After that, the only thing that you need to do is to add C:\Users\<user>\python\venv\Scripts to your Path, and you're good to go.
Caveats
By adding this folder to your Path, you may be using an outdated python version in the future, since the Scripts folder inside your virtual environment also adds a python executable that will be enabled in the path.
The recommended solution
As I stated before, I do not recommend to have the Microsoft Store version of python installed on your machine. That said, you're probably using it for the conveniences of having the latest Python version installed as soon as they're released. To alleviate this need while also getting rid of your MS Store Python. I recommend you using Chocolatey to install python (and pretty much any other programs for development).
What is Chocolatey?
Chocolatey is a package manager for Windows, pretty much like apt-get for Ubuntu Linux or HomeBrew for MacOS. By using a package manager, you get rid of the hassle of always having to execute the (mostly annoying) install wizards on windows.
To install Chocolatey:
Go to chocolatey.org/install and follow the install instructions;
(Recommended: Take a look on their documentation later to see what Chocolatey is capable of);
With Chocolatey installed, take a test drive and see if it is working properly by running choco -v in PowerShell;
By having Chocolatey installed, you can now run choco install python -y. Let's break down this command:
choco install -> The package installer of chocolatey
python -> the name of the package you want to install
-y -> This tells the installer to skip install verification by saying "Yes to All" scripts that will be executed in order to install a package.
With python installed from chocolatey, you can also see that Python is already added to your path - This means that any python package or executable installed globally will be now available on your machine!
Hope I could help you!
The above answer is good but I managed to get it to work by doing the following.
Find your installation under C:\Users\"your user"\AppData\Local\Packages it will be named something like PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
Open your windows settings in the start menu
In search type Environment variables. Edit environment variables for your account should pop up. Click it
In the top box find Path, click it
On the right Click new and enter C:\Users\"your user"\AppData\Local\Packages\"python install directory name from 1. here"\LocalCache\local-packages\Python37\Scripts inside the little box under the last item in the list
open a new cmd prompt and type the script you wanted it should work.
On Windows you can find the user base binary directory by running
python -m site --user-site
and replacing site-packages with Scripts.
For example, this could return
C:\Users\Username\AppData\Roaming\Python36\site-packages
so you would need to set your PATH to include
C:\Users\Username\AppData\Roaming\Python36\Scripts
You can set your user PATH permanently in the Control Panel. You may need to log out for the PATH changes to take effect.

Anaconda overriding python as the default site-packages directory windows 7

from what I remember of middle school, anacondas and pythons are large snakes that overpower their prey, but anacondas are much bigger and heavier, which may be how it is overtaking my python pathway:
I installed psycopg2 with an easy .exe on windows 7 last night, and it worked (to the anaconda path)- but when I tried to start a Django app, it is looking in Python27\lib\site-packages. I allowed Anaconda to add itself to the pathway when I installed it because a post recommending it said to, and I also have Python set up correctly with a windows path variable.
This makes me wonder, should I actually uninstall python2.7.10, since Anaconda came with python 2.7.9 built in, and use the anaconda prompt for everything, and also delete python 2.7 from my path variable in windows? So,exactly
What is the best way to use Anaconda with Django?
I currently spent the weekend trying to tackle a similar issue. I have Anaconda (Miniconda3) installed for analytical work, however I also wanted to work on a Django project. In the end I ended up using an Anaconda virtual environment to work on my project. Here is basically what i did in cmd:
>mkdir mysite
>cd mysite
>conda create -n mysite-env python=3
>activate mysite-env
>conda install django
Basically this created a virtualenv in the envs folder in my Anaconda installation and I can create/work with django without having to worry about multiple python installations. Not sure if this is the best way, but its the way that works for me.
The issue you are having is that you may not have looked into what Anaconda is used for or what it solves. This step was pointed out in the installation steps when it adds Anaconda to your PATH. Being that Anaconda installs Python and has "environments" there is no reason to have 2 versions of the Python installation on your system, but Anaconda allows you to have different versions of Python with environments. Only one can use the "Python" command unless you execute it from the python.exe directory or make a separate alias as the python command and point it to the Anaconda or other Python installation. Again this is pointless as you can create a new environment with your Django project and requirements.
In short, having 2 separate Python installations is pretty much taking away what Anaconda helps solve.
In Windows 10, open Anaconda Navigator, search Not installed for 'django'. Click the check box, then hit Apply.

Using (Ana)conda within PyCharm

I've got Pycharm 4 running on my Linux (Ubuntu 14.04) machine. In addition to the system python, I've also got Anaconda installed. Getting the two to play nicely together seems to be a bit of a problem... PyCharm provides some interesting integration for virtualenvs and pip, but the Anaconda Python distribution seems to prefer using its own conda tool for both activities.
Is there a relatively simple/painless way to be able to use conda in conjunction with PyCharm? Not just as an alternative interpreter i.e. point PyCharm at the Anaconda Python binary for a project interpreter, but to be able to create, source/activate and deactivate virtual envs, add/remove packages in those virtual envs, etc.
Or am I going to have to choose between using Anaconda (and having a more recent and up-to-date python than may come with the system), and being able to use PyCharm's features to their fullest extent?
I know it's late, but I thought it would be nice to clarify things: PyCharm and Conda and pip work well together.
The short answer
Just manage Conda from the command line. PyCharm will automatically notice changes once they happen, just like it does with pip.
The long answer
Create a new Conda environment:
conda create --name foo pandas bokeh
This environment lives under conda_root/envs/foo. Your python interpreter is conda_root/envs/foo/bin/pythonX.X and your all your site-packages are in conda_root/envs/foo/lib/pythonX.X/site-packages. This is same directory structure as in a pip virtual environement. PyCharm sees no difference.
Now to activate your new environment from PyCharm go to file > settings > project > interpreter, select Add local in the project interpreter field (the little gear wheel) and hunt down your python interpreter. Congratulations! You now have a Conda environment with pandas and bokeh!
Now install more packages:
conda install scikit-learn
OK... go back to your interpreter in settings. Magically, PyCharm now sees scikit-learn!
And the reverse is also true, i.e. when you pip install another package in PyCharm, Conda will automatically notice. Say you've installed requests. Now list the Conda packages in your current environment:
conda list
The list now includes requests and Conda has correctly detected (3rd column) that it was installed with pip.
Conclusion
This is definitely good news for people like myself who are trying to get away from the pip/virtualenv installation problems when packages are not pure python.
NB: I run PyCharm pro edition 4.5.3 on Linux. For Windows users, replace in command line with in the GUI (and forward slashes with backslashes). There's no reason it shouldn't work for you too.
EDIT: PyCharm5 is out with Conda support! In the community edition too.
Change the project interpreter to ~/anaconda2/python/bin by going to File -> Settings -> Project -> Project Interpreter. Also update the run configuration to use the project default Python interpreter via Run -> Edit Configurations. This makes PyCharm use Anaconda instead of the default Python interpreter under usr/bin/python27.
as per #cyberbikepunk answer pycharm supports Anaconda since pycharm5!
Have a look how easy is to add an environment:
Continuum Analytics now provides instructions on how to setup Anaconda with various IDEs including Pycharm here. However, with Pycharm 5.0.1 running on Unbuntu 15.10 Project Interpreter settings were found via the File | Settings and then under the Project branch of the treeview on the Settings dialog.
this might be repetitive.
I was trying to use pycharm to run flask - had anaconda 3, pycharm 2019.1.1 and windows 10. Created a new conda environment - it threw errors.
Followed these steps -
Used the cmd to install python and flask after creating environment as suggested above.
Followed this answer.
As suggested above, went to Run -> Edit Configurations and changed the environment there as well as in (2).
Obviously kept the correct python interpreter (the one in the environment) everywhere.

Categories

Resources