Installing third party modules in python3 - Ubuntu - python

In short, my question is, how do I install the latest version of scikit-image into my usr/lib/python3/dist-packages so I can actually use it? I think there is a problem with my understanding of how third-party modules are installed. As a newb, I don’t know how to rectify that, hence this post.
I need help to understand how to install packages in python3 up until now I have used pip/pip3/apt-get/synaptic etc and it has worked fine for many packages. However, I have hit several barriers (Skimage, opencv, plantcv in python3). I must emphasise, the problem I am having is using these packages in python3, not 2.7.
For example, I want to use the latest version of scikit-image (0.14) with python3. (http://scikit-image.org/) I have tried using the installation instructions and have not yet successfully managed to install it. I have navigated to my usr/lib/python3/dist-packages and copied scikit-image into this directory (I have all the dependencies installed in here already).
Image of my folder for dist-packages as proof
As you can see, the folder containing skimage is in the directory I want to be installed in, how do I actually install it? Do I have to extract skimage out of the folder into the directory and then run the install command? If I navigate to usr/lib/python3/dist-packages/scikit-image and then run pip install -e . I get an error, stating that I need numpy. If I write a python script using python3 I can clearly see I have it installed (and I have been using it for a long time). So, there must be a problem in how I have this package in my file system. I think a janky workaround would be to copy all the modules into my working directory and Import them that way as if they were modules I have made myself, but this obviously negates the whole point of installing packages.
This has also happened with another package called plantcv. Where I went into the directory usr/lib/python3/dist-packages then cloned the source from git hub and installed as per instructions. When I import plantcv in my python3 script. It Imports fine. But, there is nothing in it, as python cannot see the modules which are inside this folder at usr/lib/python3/dist-packages/plantcv/plantcv.
There is clearly some comprehension here that I am missing, as I have a similar problem for two packages now. Please, Internet. Help me understand what I am missing!

You simply need to copy the folder in /usr/lib/python3/dist-packages/package-name
However, there are certain things that are specific to python packages. The folder named package name should be a valid package. A good indicator of that is it will contain a file "__init__.py". It is very likely that every sub-directory inside this package directory will contain a "__init__.py" file. It depends on whether there are modules inside these sub-directories.
In your code simply import the package like the following.
import package-name
where package-name can be skimage

Related

How to move site-package from folder Python37 to folder Python310?

Recently, in I download and install Python 3.10, then uninstall Python 3.7 in win10.However, when I want to import some modules in script, it shows there is no modules. After checking, I found all modules are still saved in C:\Users\...\AppData\Roaming\Python\Python37
So if I want to use them, just copy/paste them into Python310? Need I change other things, like classpath...
If not, what should I do? download from Internet again?
Don't copy site site-packages dir from one version of Python to another - at best you'll get undefined behaviour. The appropriate way of getting the modules you want from v3.7 to v3.10 is to run pip list from the v3.7 installation to see what you have installed, then pip freeze to write that to a file, and finally use the v3.10 pip to install from the file that you wrote the list to.
This way you will get the modules built with and for v3.10.

Pip install doesn't install any .py files

The issue: I ran pip install pyshp, but could not import it into my python project. When I check the package in my site-packages folder, I see it installed as 'pyshp-2.1.0.dist-info'. When I look at what's inside that folder, I only see the following files: INSTALLER, LICENSE.txt, METADATA, RECORD, REQUESTED, top_level.txt, and WHEEL. I don't see a single .py file. When I download the package manually from the pyshp download page, the folder has a bunch of other files, including the shapefile.py file that I need. So it seems like the download is getting stuck somehow. I've also tested and encountered this same issue with Pillow and Matplotlib, so I think it's something with my Python setup, not an issue with the package itself.
I'm fairly new to Python, does anyone know what I've done wrong? Or any other ideas? Thanks for your help in advance.
Edit: To be clear, I'm not trying to fix the pyshp install issue directly, but instead figure out why the install process isn't working for all of the packages I'm trying to install.
Go to your site packages directory, it is in the root of the site packages directory. So copy and place it in the same directory beside your python script, and you can import shapefile

OpenCV built from source: Pycharm doesn't get autocomplete information

I'm trying to install OpenCV into my python environment (Windows), and I'm almost all of the way there, but still having some issues with autocomplete and Pycharm itself importing the library. I've been through countless other related threads, but it seems like most of them are either outdated, for prebuilt versions, or unanswered.
I'm using Anaconda and have several environments, and unfortunately installing it through pip install opencv-contrib-python doesn't include everything I need. So, I've built it from source, and the library itself seem to be working fine. The build process installed some things into ./Anaconda3/envs/cv/Lib/site-packages/cv2/: __init__.py, some config py files, and .../cv2/python-3.8/cv2.cp38-win_amd64.pyd. I'm not sure if it did anything else.
But here's where I'm at:
In a separate environment, a pip install opencv-contrib-python both runs and has autocomplete working
In this environment, OpenCV actually runs just fine, but the autocomplete doesn't work and Pycharm complains about everything, eg: Cannot find reference 'imread' in '__init__.py'
Invalidate Caches / Restart doesn't help
Removing and re-adding the environment doesn't help
Deleting the user preferences folder for Pycharm doesn't help
Rebuilding/Installing OpenCV doesn't help
File->Settings->Project->Project Interpreter is set correctly
Run->Edit Configuration->Python Interpreter is set correctly
So my question is: how does Pycharm get or generate that autocomplete information? It looks like the pyd file is just a dll in disguise, and looking through the other environment's site-packages/cv2 folder, I don't see anything interesting. I've read that __init__.py has something to do with it, but again the pip version doesn't contain anything (except there's a from .cv2 import *, but I'm not sure how that factors in). The .whl file you can download is a zip that only contains the same as what 'pip install' gets.
Where does the autocomplete information get stored? Maybe there's some way to copy it from one environment to another? It would get me almost all the way there, which at this point would be good enough I think. Maybe I need to rebuild it with another flag I missed?
Got it finally! Figures that would happen just after posting the question...
Turns out .../envs/cv/site-packages/cv2/python-3.8/cv2.cp38-win_amd64.pyd needed to be copied to .../envs/cv/DLLs/. Then PyCharm did it's magic and is now all good.
Alternatively add the directory containing the .pyd file to the interpreter paths.
I had exactly this problem with OpenCV 4.2.0 compiled from sources, installed in my Conda environment and PyCharm 2020.1.
I solved this way:
Select project interpreter
Click on the settings button next to it and then clicking on the Show paths for selected interpreter
adding the directory containing the cv2 library (in my case in the Conda Python library path - e.g. miniconda3/lib/python3.7/site-packages/cv2/python-3.7). In general check the site-packages/cv2/python-X.X directory)

Install and Import Modified Package

I need to make some changes to the way the OSMnx package gets data from the Overpass API.
To do that, I've forked the repo and cloned that fork into a local directory.
I've made a simple change, adding a print functions to start.
I'm trying to figure out the steps for installing and importing the modified version of the package. I've looked at this question
I'm concerned about a namespace conflict, should I change the name of the folder or the name value in the setup.pyfile. I don't want to write over the working version of the package
What file should I point to when installing? setup.py, core.py, _init_.py?
I haven't found any tutorials on how to modify an existing package safely, just stuff on how to make a package from scratch.
import will search the installed packages list first, then it will check the directory it was called from for files that match the package it's looking for.
So either using pip or conda remove the original version of the package being imported.
Then, be sure that the modified version of the package that you want to import is in a sibdirectory of your project directory, and run import package as xx and it should load the modified package files.
If it doesn't, it's likely that the package wasn't removed from the right environment.

Why install Python packages

Why we have to install the python packages before using them?
I am currently working on a small python mysql program. What i tried to download the python connector module from mysql webpage and simply unzip it and place it in the same folder of my code.
And I can import the module properly.
So what is the meaning of installing those packages? Can I use those packages like matplotlib, numpy without installing them ?
Is it possible to have all the required packages installed on a folder so that i can move it to another computer and run my program with only CPython installed (I don't want to install any package on this computer)?
it's not that simple :-)
some packages have dependencies, you also need to download and extract their dependencies (you need pacakge x,and package x uses y) pakcage manager handles that
some package have some c code (they need to be compiled before use (ujson or postgres module) package manager handles that
when your share your code instead of sharing dependencies you simply add a file containing the list of dependencies (requirements.txt) and other user can simply install all dependencies using package manager
Installing a python package enables us to use it anywhere on our system. If we just place the package in the same directory as our script then it may well work, but only for scripts in that directory.
Some packages also rely on others to function properly, and the installation of a package may well install those pre-requisite packages for you. You may be able to do this manually, but you'd have to put them all in the same directory as your script every time you wanted to run it.
So installing the packages is the easiest way to use them.
You don't have to install them, and in some cases you wouldn't install them on your system; if you had split your code across two files and imported one file at the top of the other for example.
In fact, you don't really need install package on your system.
But if you install it, you can use these packages every where on your system.
Also, you can create a requirement.txt file to enable install all packages that you need on other computer. You can check this manual https://pip.pypa.io/en/stable/user_guide/#requirements-files

Categories