Packages installed with Poetry fail to import - python

Having a simple yet confusing issue: a package I added with poetry fails to import when I try to use it in a module. Steps taken:
poetry add sendgrid
In a module, import sendgrid
Error: Import "sendgrid" could not be resolved PylancereportMissingImports
Troubleshooting I've tried:
I checked my project's poetry venv dir, and sendgrid is there: 'C:\\Users\\xyz123\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\nameofproject-py3.10\\lib\\site-packages'
Also checked sys.path(); the path to that site-packages dir is listed
Running poetry install gives me the response No dependencies to install or update
both the pyproject.toml and the poetry.lock files list sendgrid
What is going on?

Well, it turns out it's a matter of VSCode not playing nice and failing to recognize Poetry's virtual environment. I had to run the Python: Select Interpreter command and change the venv directory to the one my project is using, then it was able to recognize the installed packages.
See here for more details on how to do that.

Related

How to install Python package from GitHub that doesn't have setup.py in it

I would like to use the following sdk in my python project -> https://github.com/LBank-exchange/lbank-api-sdk-v2. It has sdk's for 3 languages (I just want the python one). I tried to install it using the command:
pip install git+https://github.com/LBank-exchange/lbank-api-sdk-v2.git#egg=lbank
which gave the error
does not appear to be a Python project: neither 'setup.py' nor 'pyproject.toml' found.
Looks like the developer didn't bother to package it properly. It it was me using it, I would fork it on GH, add the setup.py and use the fork. Maybe a good exercise for you?
Meanwhile, to just get it to work, in your project "root":
git clone https://github.com/LBank-exchange/lbank-api-sdk-v2.git
ln -s lbank-api-sdk-v2/python-sdk-api/LBank ./LBank
Then in your code just import LBank. This will leave the cloned repo untouched (so you can git pull to update it later) and just link the module directory to the root. Alternatively you can just include the api directory in sys.path for imports to work.
Think there is nothing to install, if you want to be able to "import" and use it like other packages you install through pip install you can just add the folder to your sys-path:
import sys
sys.path.append("path")

`ModuleNotFoundError` When Importing Python Package from Git

I'm attempting to install a featurestore package from a private GitHub repo via ssh with the following command:
pip3 install -U git+ssh://git#dsghe.<mydomain>/bshelton/package_test.git#master#egg=featurestore
The install is successful, per the image below:
But when trying to run from featurestore import *, I get a ModuleNotFoundError: No module named 'featurestore' error.
Using pip3 freeze, I see that the package is installed, but not with the <package>==<version> syntax I would expect, but it seems to be referencing the git commit instead as its "version":
I believe that the repo's directory set-up is appropriate for a Python package, per the screenshot below.
A noticeable difference between this package's install and the other packages I've installed is that it seems like only the ...dist-info folder is installed for my featurestore package, while every other installed package includes the actual package directory, in addition to the ...dist-info folder. Using ls ~/.local/lib/python3.6/site-packages:
This is my first time, trying to create a package like this, and I've been referencing the several sources below, but would appreciate some insight from the community as to what I'm missing. Thanks.
https://packaging.python.org/tutorials/packaging-projects/
pip install from git repo branch
The cause of my issue turned out to be a syntax error in my setup.cfg above the package_dir and packages lines, which are important if using a package layout with 'src'. Until I fixed the error, my package installed and imported fine in some contexts but not others
I was able to solve for this by moving my featurestore directory up one level, and getting rid of the src directory. Based on the top answer here, I may have been able to also solve for it by simply adding a __init__.py file directly in the src directory. But for my need, src was really an unnecessary level.
New package directory set-up:
Code ran in terminal:
!pip3 install -U git+ssh://git#dsghe.<mydomain>/bshelton/package_test.git#master#egg=featurestore
from featurestore.featurestore import *

How to import module installed by pip to a custom directory

I'm writing a plugin for an app, and found that packages I install with pip are apparently being ignored, so I'm trying to install them within my plugin's directory using
pip install --ignore-installed --install-option="--prefix=[plugins-folder]" [package-name]
This creates a folder structure lib/python2.7/site-packages/[dependencies] which is fine by me, except I can't figure out how to them import those dependencies. Even if I manage to import the main package, it will break because it can't find it's own dependencies which are also in that same directory structure.
I suggest that you should use virtual python environment. virtualenv for python2/python3 and python3 -m venv for python3.
You python environment seems orderless and you should isolate each environment for each python app.

Pip Wheel Package Installation Fail

I try to run pip wheel azure-mgmt=0.20.1, but whenever I run it I get following pip wheel error, which is very clear:
error: [Error 183] Cannot create a file when that file already exists: 'build\\bdist.win32\\wheel\\azure_mgmt-0.20.0.data\\..'
So my question is where or how I can find that path? I want to delete that existing file. I have been searching my local computer, searched for default path in Google, but still didn't find any solution.
Also is it possible to tell pip wheel to output full log? As you can see that full error path is not displayed. I'm using virtualenv.
We can see the description of virtual env at the official python guide:
To help manage external package dependencies, Azure Git deployment supports the creation of virtual environments.
When Azure detects a requirements.txt in the root of the repository, it automatically creates a virtual environment named env. This only occurs on the first deployment, or during any deployment after the selected Python runtime has changed.
You can directly modify the dependencies and versions of them in requirement.txt, then deploy your python app to Azure Web App via git, Azure will update the python packages automatically. You can check the packages in the virtual env folder which path is env\Lib\site-packages in the root directory of your site. You can login on the kudu console of your site to check your files of you site online, the URL should be: https://{your_site_name}.scm.azurewebsites.net/DebugConsole .
Additionally, according your description, it seems that you use the global python environment to run pip install command which may directly install packages in your global python environment. To install packages in your virtual env, you need to run the similar command env\scripts\pip install -r requirements.txt in your root directory of your application. Please refer to Web app development - Windows - command line for more information.
have you tried uninstalling and reinstalling?
I tried pip wheel azure-mgmt and that installed -0.20.1 for me.
The directory for mine is /Users/me/wheelhouse, so you could look there. I found that in the initial log of the build.
#Amir,
One option is that you could generate the requirement.txt file and remove your virtual environment if you used Visual studio to develop your application. Then you can add a new virtual environment for your project and install all packages from requirement.txt file. Or after removed your virtual environment, you can try pip wheel azure-mgmt command.
And another option is that you can follow this similar issue:https://vilimpoc.org/blog/2014/01/18/time-robbing-python-errors/
The blogger modified the LOCALAPPDATA path to resolve this issue. Please try it.

Pythonic Ways of Importing Custom Modules?

I've needed to deal with this for some time, but never really figured out what the most pythonic way of importing/setting up PYTHONPATH for custom modules is. I know I can use virtualenv to manage it, I know I can set it inside of scripts, or through pth files, but none of these seem very clean and pythonic to me, so I'm guessing I'm missing something.
Almost always, all custom modules I'm interested in are contained in the git directory I've cloned down that has whatever script I'm running, if that simplifies things.
I'm guessing virtualenv is the answer, but figured I'd ask in case I'm missing anything.
EDIT: To clarify, this is only a question about custom modules. I'm already using pip for modules from PyPI.
You can use pip to install packages that are not on PyPI also. You just need an URI endpoint and a valid python package:
Examples:
$ pip install https://github.com/pypa/pip/archive/develop.zip#egg=pip
$ pip install git+https://github.com/pypa/pip.git#egg=pip
$ pip install git+git://github.com/pypa/pip.git#egg=pip
$ pip install /path/to/pip.tar.gz
$ pip install .
Read more at https://pip-installer.org/en/latest/usage.html#pip-install
virtualenv is a good start.
There are also package managers like pip and easy_install that manage third party modules.
In code you can use:
import sys
sys.path.append('/path/to/customModule')
Virtualenv is the way to go with this.
pip install virtualenv
Then make a folder to setup your environments. Inside that folder:
virtualenv <new_env_name>
That'll create a new folder in that directory, inside that there's a bin folder, run source on activate in that bin folder. You can then do pip install and it will only install it for that environment.
If you're cloning a git repo that you also want to be able to peruse the code easily (like if you're also working on that repo) clone it into your work_dir and then symlink or alias the package folder into the site-package directory inside that virtualenv's lib directory. Otherwise, if it's packaged correctly if you do python setup.py install it should install it right for that virtualenv.

Categories

Resources