Python virtualenvironment can't find packages - python

When i run python -m pip install {package} inside the virtual environment i created with python -m venv env the package is installed inside the env/Lib folder, however when i want to use the package the venv interpreter can't find the package. For example I installed ipykernel, but when I try to run a cell inside Visual Studio code I get a message saying I have to install ipykernel and even if I accept this message and install it again I keep getting the message that I have to install ipykernel.
I already tried deleting all my versions of python and reinstalling python I also tried to set the global target of python to my library folder python pip config set global.target {path to venv Library}
I am using python 3.8.0 which is the only python installed on my pc. I also have windows 10 and my editor is visual studio code. Please help.
If I run python -m pip freeze requirements inside my enviroment nothing happens. When I run python -m pip freeze requirements outside my environment I get a list of all installed packages

Please enter the command "pip show ipykernel" in the terminal of VS Code to check whether this module is included in this environment. (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 terminal shows that there is no such module, please open a new VS Code terminal(shortcut key: Ctrl+Shift+`), it will automatically enter the environment displayed in the lower left corner of VSCode.

Related

"Import "youtube_dl" could not be resolved" Using virtual environment / pip, all other imports work

screenShot of vsc
A screenshot of my visual studio code is above in the link ^.
I have a virtual env running in the terminal in vsc. I have the vsc python interpreter set to python 3.10.7. I have been using pip to install dependencies. All other dependencies work, ie flask, tensorflow, numpy, etc. But youtube_dl says it cannot be resolved. It is installed in the location shown: c:\users\user\onedrive\desktop\projectName\.venv\lib\site-packages
Any idea why this is not resolving?
I have tried doing pip install youtube_dl within the virtual env. I have tried it without of the virtual env, I have tried using python3 -m pip install youtube_dl. Not sure why this won't resolve.
Please note that Python 3.10.7 in the lower right corner is not consistent with the python version you understand.
It is Python Interpreter instead of python version.
This problem is caused by the wrong python interpreter. You installed the package in the .venv environment and run the file at the base environment.
The solution is using shorcuts "Ctrl+Shift+P" and typing "Python: Select Interpreter" to choose your .venv environment. (You can also click Python 3.10.7 in the lower right corner).
You Can click on the "3.10.7 64-bit" and vscode will show you selected python environment and then make sure that the chose env is your '.venv'.
You can also run you code on terminal with activated .venv and there should not be a Problem.

PIP version VSCode

I am having troubles trying to understand why VSCode uses a different pip version.
I have two python versions installed in Windows, python38 and python39, if I open the win 10 terminal I got pip version 20.2.4 when running pip3 --version, but if i run the same command inside the VSCode terminal i got pip 20.2.2 from c:\python38\lib\site-packages\pip (python 3.8).
Both terminals are running PowerShell 7.0.3 on the same profile.
Also removing python38 from the global PATH variable gives command not found error only in vscode, in windows terminal keeps working as i described before.
Could not fix this so I deleted all temporary and profile data of vscode and can now run pip from within its terminal.
Try changing your interpreter: ctrl+shift+p->select python interpreter and choose the one you want because pip is part of the python environment, and therefore the version of pip you are using is tied to the active python interpreter (cc #DragonBobZ)
It's all about the Process PATH. Check $Env:PATH in both. Starting from the left, the first folder with pip3.exe is used. You have probably selected a python interpreter for VS Code, which then adds it in the PATH.
You can also check the location of the used pip3.exe with Get-Command pip3. (might be helpful in debugging)
It appears something got fixed after deleting all profile and settings data from visual studio code, it runs pip now form the terminal inside vscode.

Installed modules not found by Python

I need to use the autogui module to do something in Python. When I run the script, it says that it can't find the autogui module so I installed it with
pip install autogui.
But when I run the script again it still says me this module doesn't exist.
Method 1:
You're probably having trouble setting up your correct Python Interpreter and working within it,try the following in VSCode.
Ctrl + Shift + p
And enter the following in the field.
python: select interpreter
Select the desired environment and reinstall PyAutoGui
Method 2:
Creating a virtual environment for your project where all your packages will be installed and will be isolated from others, and will have no import errors since it's an environment specifically for the project that you're working on.
I assume you use Windows, so open the command line in your working directory, or open your working directory in VSCode and enter the following in the command-line tool that is provided within VSCode.
The Python installers for Windows include pip. You should be able to access pip using:
py -m pip --version
You can make sure that pip is up-to-date by running the following
py -m pip install --upgrade pip
Installing virtual environment
py -m pip install --user virtualenv
Creating a virtual environment
py -m venv env
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.
venv will create a virtual Python installation in the env folder.
Finally, to activate the environment run the following command
.\env\Scripts\activate
That will activate your environment.
pip install pyautogui
Make sure to change your interpreter to the one that you just created in the env/bin folder and run your code, or you could just enter the path to the *python file located in the env/bin folder.
Try pyautogui - I had the same problem. Instead of autogui, write `pyautogui. Or, if you are running python3 or higher then try:
pip3 install pyautogui.

VS code not recognize the Python package I installed on my Mac

I tried to run a python script file on Visual Studio. But the terminal keep pop up saying that I don't have the package installed... I don't know what is going on. I installed Python through homebrew and install all the package through pip3. I even find the path for all the package I have installed, and it's still not working.. I switched between multiple interpreter, and none of them worked, all said my package is not installed.. but it's clearly my package is installed... I even create a new py file to run pandas, and it worked.. Can someone help me with this? This is driving me crazy..
It just because you had chosen the wrong python environment in VSCode.
You said you had created a python file to run pandas and it worked. This means you had installed the package 'pandas' and it worked. But you haven't chosen this environment in your VSCode.
In the terminal:
By command "pip3 --version" to check which pip you are using.
By command "python3" -> "import sys; sys.executable" to check which python you are using.
By command "python3" -> "import sys; sys.path" to check which 'site-package' the interpreter searching for.
From your description, you are using the pip which under '/usr/local', install the packages under '/usr/local/lib/python3.7/site-packages', using python interpreter of '/usr/local/bin/python3'. This is the default python interpreter because of the system environment variables settings. But in VSCode you haven't chosen this environment.
Solution:
Choose an environment or create a virtual environment(recommend). Activate the environment(Ctrl+Shift+`), by the command 'pip3 --version' to check which pip3 you are using, then install the package you want to import.
If you installed python via homebrew try using homebrew to install pandas instead of pip. I've seen things before that they get linked. I would just recommend using pip to uninstall pandas first so there is only package.

Python 3.8 on Windows 10/Linux (Manjaro 18.1.5): ModuleNotFoundError for TestPyPI package installed with pip

I followed along with the package distribution tutorial for uploading a package on test.pypi.org from a Linux machine, and proceeded to try to install the package on a separate Windows machine.
On the Windows machine, I installed Python 3.8 and ticked the Add Python 3.8 to PATH box. No previous Python installation was present, and it installed to C:\Users\me\AppData\Local\Programs\Python\Python38\.
In Environmental Variables, my PATH under "Users variables" is: C:\Users\me\AppData\Local\Programs\Python38\Scripts\;C:\Users\me\AppData\Local\Programs\Python38\;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps.
My PATH under "System variables" is much longer, but it contains no Python directories.
I then downloaded and installed pip.
In Command Prompt, I first created a virtual environment with py -m venv tutorial and activated it with Scripts\activate. Activation was verified by the (tutorial) next to the prompt. Then, in the virtual env, with py -m pip install ... I installed my example package.
I can verify it's installed by seeing it in the output of py -m pip list:
Package Version
----------------------- -------
example-pkg-me 0.0.1
pip 19.2.3
setuptools 41.2.0
example-pkg-me does not appear outside the virtual env.
The tutorial finishes by having you attempt an import in the Python interpreter:
py
>>> import example_pkg
but I get ModuleNotFoundError: No module named `example_pkg`. Why? Is something wrong with either of my PATHs? Note that the tutorial specifically mentions omitting the username (and import example_pkg_me issues the same error regardless).
Curiously, when trying to install and import a "mainstream (?)" package, it succeeds with no errors. I.e.,
py -m pip install numpy
and
py
>>> import numpy
works.
Update:
Same results on a separate Linux machine with the same steps where the only differences are that Python and pip were already present.
As an aside, if your package is intended to be run (as a GUI app) rather than imported in the interpreter as in the tutorial, how would the person who installed it run it? Would they have to navigate to the directory where the package files were physically installed and run a main.py script there? Is there a better way (like freezing the package as an executable with the likes of cx_Freeze and distributing the EXE instead of relying on pip installations)? Or maybe just expect them to download a repository, run py setup.py install, and then a main.py script?

Categories

Resources