Pip install and run git repo - python

I'm trying to pip install a separate git repo into my python project. Pip install seems to work when I run pip install git+https://github.com/XxdpavelxX/myapp. However when I then run my code I get the following error.
Here's my app: https://github.com/XxdpavelxX/myapp
ModuleNotFoundError: No module named 'myapp'
ERROR: could not load /Users/myUser/stuff/callerFile.py
Here's the callerFile.py (in a separate git repo):
from myapp import test
print test.random_print()
I suspect that this is pip install related. When I run pip install git+https://github.com/XxdpavelxX/myapp it seems to pass, however inside of my python venv/lib/python3.7/site-packages I only see myapp-1.0py3.7.eggs-info instead of the actual package. Anyone knowing what I'm doing wrong? Do I need to add my library to pypi for this to work?
Edit: Added the actual url to github repo I'm testing.

Create a folder called myapp and move the __init__.py and test.py files to that folder.
Add the following line to your setup.py (I added after url),
packages=['myapp'],
Now installation will be successful and you can import your package.
What is setup.py?

You don't need to post your code to pypi.
I suggest you to use tag #egg to set package name.
So the pip status would be like
pip install git+https://github.com/myGitUser/myLibrary#egg=myLibrary

Your package has neither py_modules nor packages hence it doesn't install anything importable when installed.
My advice is to rename your __init__.py to myapp.py and add this to setup.py:
setup(
…
py_modules=['myapp'],
…
)

Related

Packages installed with Poetry fail to import

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.

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 do I install a package from GitHub gist?

I don't understand this...
I want to install this https://gist.github.com/sixtenbe/1178136.
It is a peak detection script for python.
Everywhere I look I am told to use pip with the .git extension.
All I see is how to download the .zip, but from there I am lost.
How can I install this?
Thanks.
You can get the individual files in the Gist (or download the Gist as an ZIP and extract) and put them in your source code folder.
Then you will be able to import them as modules in your own scripts:
import analytic_wfm as AW
AW.ACV_A6( ... )
import peakdetect as PK
PK.peakdetect_parabola( ... )
Let's give it another look.
By "installing a package" we might mean that the package should be available via import.
For that the package directory should reside either in the current directory or in one of the other directories in the import search path.
One such directory is the "user-specific site-packages directory, USER_SITE":
python -c "import site; print(site.getusersitepackages())"
Git URL
First we might need a Git URL. Going to https://gist.github.com/sixtenbe/1178136 we can click on the Embed pop-up and switch it to Clone via HTTPS:
in order to obtain the GIT URL: https://gist.github.com/1178136.git.
git and bash
Having the Git URL and the Unix shell (bash) we can install the package manually into the USER_SITE.
Let's go into the USER_SITE first:
cd $(python -c "import site; print(site.getusersitepackages())")
pwd
Now that we are in the USER_SITE, let's download the Gist:
git clone https://gist.github.com/1178136.git analytic_wfm
Finally, let's verify that the package is now available:
cd && python -c "import analytic_wfm.analytic_wfm; print(analytic_wfm.analytic_wfm.__all__)"
If numpy is installed, it prints
['ACV_A1', 'ACV_A2', 'ACV_A3', 'ACV_A4', 'ACV_A5', 'ACV_A6', 'ACV_A7', 'ACV_A8']
pip
Let's try to install a Gist package with pip.
For pip install we should prefix the Git URL with git+:
pip install --user git+https://gist.github.com/1178136.git
This gives us the error:
ERROR: git+https://gist.github.com/1178136.git does not appear to be a
Python project: neither 'setup.py' nor 'pyproject.toml' found.
Looks like the package we've picked is missing the necessary pip configuration!
Let's try another one:
pip install --user git+https://gist.github.com/bf91613a021a536c7ce16cdba9168604.git
Installs NP:
Successfully built llog
Installing collected packages: llog
Successfully installed llog-1.0
Particularly because it has the setup.py.
Note also that Gist does not support subfolders, and pip seems to depend on them in handling the packages argument, but the code in setup.py can workaround this by creating the package subfolder on the fly and copying the Python files there!
Hence if you want to import that Gist, https://gist.github.com/sixtenbe/1178136, with the rest of the requirements.txt dependencies, - you can fork it and add setup.py to the effect.
pypi
Given that the analytic-wfm can also be found at the Python Package Index, https://pypi.org/project/analytic-wfm/, you can install it with
pip install analytic-wfm

Install local dist package into virtualenv

I have a pytest test, let's call it test.py. I used to run this test outside of virtualenv; now I'm trying to run it inside a virtualenv sandbox.
The project is structured like this:
~/project/test # where test.py and all virtualenv files live
~/project/mylibrary
test.py imports from mylibrary. In the past, this worked because I have the code in ~/project/mylibrary installed into /usr/lib/python2.7/dist-packages/mylibrary.
I can't run virtualenv with the --system-site-packages flag. I also can't move the code from ~/project/mylibrary into the ~/project/test folder. How can I get access to the code in mylibrary inside my virtualenv?
You don't need to do anything special - as long as you are working inside a virtualenv, python setup.py install will automatically install packages into
$VIRTUAL_ENV/lib/python2.7/site-packages
rather than your system-wide
/usr/lib/python2.7/dist-packages
directory.
In general it's better to use pip install mylibrary/, since this way you can neatly uninstall the package using pip uninstall mylibrary.
If you're installing a working copy of some code that you're developing, it might be a good idea to install it in "editable" mode using pip install -e mylibrary/, which creates a link to your source directory so that your installed module gets updated as you edit the code.
The easiest way would be to add the directory containing the library to your sys.path

Categories

Resources