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

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.

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.

Python VSCode: Installed modules not recognised (closed)

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

Fails to prompt "Python version" and "pip list" after creating virtual environment with "virtualenv"

I want to create a virtual environment to install modules only required for a particular proj and for sharing whole package.
Versions that are installed in my system
Python 3.8.3 -- envvar set to python3
PyCharm 2020.1.2 (Community Edition)
Python 2.7(due to maya pyside2) -- envvar set to python
As per python docs
https://docs.python.org/3/library/venv.html#creating-virtual-environments
Deprecated since version 3.6: pyvenv was the recommended tool for creating virtual environments for Python 3.3 and 3.4, and is deprecated in Python 3.6.
Changed in version 3.5: The use of venv is now recommended for creating virtual environments.
I require PyQt5 for my current proj
and python3.8.3 was best suited so decided to go with it
and pycharm as my IDE
1) so i decided to create a virtual env through it but it prompts an error.
in case if the image is not displayed error was
2020.1.1\plugins\python-ce\helpers\packaging_tool.py pyvenv
Error:[WinError 2] The system cannot find the file specified
I don't get it why pycharm is not able to recognize the version of python3.5 and above.
2) so as per above docs i started searching venv for couple of hours and came across many pages eg: https://www.c-sharpcorner.com/article/steps-to-set-up-a-virtual-environment-for-python-development/
and referred the page and installed through pycharm the virtualenv and virtualenvwrapper-win and began the process again but this time through terminal and worked like a charm. env was created.
but as we know the python.exe is required in scripts folder to run anything but that was missing
3) that brings me to 3rd point i enforced the python persion with virtualenv path/venv -p python3.exe but still no sign of python in the environment.
and if i activate the environment "activate.bat" works fine
pip list in terminal
prompts me
Fatal error in launcher: Unable to create process using '"D:\All_Projs\Maya_Projs\succinct_save\venv\Scripts\python.exe" "D:\All_Projs\Maya_Projs\succinct_save\venv\Scripts\pip.exe" list': The system cannot find the file specified
Help appreciated
Thank You.

Sometimes Unable to Import NumPy

When I work in Jupyter Notebooks everything works fine, and I can import numpy and pandas successfully. However, when I try to download the script and then run it in an editor such as PyCharm or Atom, I get an import error: no module named numpy, and the same for pandas. How do I fix this? Is this due to the packages being installed in a different location than where I am downloading the code? Everything is installed with Anaconda, and when I try to do ```conda install numpy`` it tells me that all packages have already been installed.
This may be because Pycharm and Atom are using your default python install rather than your anaconda python environment.
You can configure Pycharm to use your conda environment via (https://www.jetbrains.com/help/pycharm/conda-support-creating-conda-virtual-environment.html).
Anaconda uses virtual conda environments to store the Python interpreter and libraries. If this isn't set up in your IDE, it won't see the libraries. This is described in this post: Use Conda environment in pycharm
Check your PyCharm interpreter options: File > Settings > Project > Project Interpreter. Make sure your desired Anaconda interpreter/environment is selected.
If your Anaconda environment isn't selected, click the Project Interpreter drop-down. if you see it there, select it. If not, click Show All... then + (Add) and browse to the Anaconda folder.
This post describes how to set up conda in Atom:
Using anaconda environment in Atom

Windows: How to configure multiple versions of Python to work together?

I installed Python 2.7 a long time ago on my PC (I am running Windows 10). Today I decided to install Python 3.7, but after typing 'python' into the command prompt the console printed 'python 2.7...'
I have no idea as to what I should do. Would this be a problem with my path configuration? I considered uninstalling python 2.7 but I still want it installed on my computer.
Configure multiple Python versions on Windows
Python 3.3 introduced the Python Launcher for Windows. Rather than using python.exe, call py and select the version with flags:
py -2.7
py -3
System-wide installations of Python 3.3 and later will put the launcher on your PATH.
Note on Virtual Environments (new in Python 3.5)
If the launcher is run with no explicit Python version specification, and a virtual environment (created with the standard library venv module or the external virtualenv tool) active, the launcher will run the virtual environment’s interpreter rather than the global one. To run the global interpreter, either deactivate the virtual environment, or explicitly specify the global Python version.
Outdated Method (not recommended)
Rename executables - After installing multiple versions, change the names of the executables. For example, for Python 2.7, under the installation folder, rename python.exe to python27.exe and rename Python 3.7 from python.exe to python37.exe. Then on the command line, select the version by entering python27 or python37. Whichever version is preferred, could be left as just python.
Add Path Environment Variables - For example, on Windows 10, go to the Windows menu and search for "environment variables" and click edit the system environment variables. In the System Properties dialog, click Environment Variables.... Under "System variables", select "Path". Click Edit.... Click New and add the first entry below. Click New again for each Path variable entry.
C:\Python27
C:\Python27\Scripts
C:\Python37
C:\Python37\Scripts
This will enable Python and pip. Be sure that paths match your actual installation directories.
I would suggest using pyenv
I have been using it and is working well for me. Some of the handy features of pyenv are
It allows installing multiple version on python easily
It allows switching python version with one command in global, shell or folder level
It also allows creating virtual env using virtualevn extension
I would suggest to use virtual environment. Soon or later you would you might get dependency problems.
You Can Configure The Python2 & python3 In Windows PC Like This:
First Of All Install Python 2 & Python 3 in windows PC in C directory like this:
C:\Python27 --> For Python2
C:\Python39 --> For Python3
After Installing Both Packages Go To Their Respective Folders And Copy python.exe & Paste In The Same Directory and rename the python - Copy with python2(In python27 folder) & python3(In python39 folder)
Python2:
Python3:
And Then Set Environment Vairable Like This:
All Done Now You Can Run Any Script Which May Compatible With Python2 or Python3 :)

Categories

Resources