Install packages for python interpreter of specific software - python

I have a question regarding installing a python package like pathlib2 for the python interpreter that comes with a software called Nuke for example.
In this case the python executeable is located in the following directory
C:\Program Files\Nuke11.1v6\python.exe
Because it is shipped by a piece of software, it looks a little different to your default python installation, at least for my knowledge. For example, I believe the modules are located inside pythonextensions\site-packages and I cannot find the typical "Scripts" folder in which pip.py should be.
The closest thing I found is C:\Program Files\Nuke11.1v6\lib\ensurepip\_bundled with pip-9.0.1-py2.py3-none-any.whl and setuptools....... but so far nothing else
What I tried:
I cd'ed into the Nuke folder and ran python.exe -m pip install pathlib2 which returned with the error No module named pip, as predicted.
I am sure it is bad practice to change the python interpreter of a software but I have no other option. Therefore I would like to modify the python version as less as possible (like using the pip it comes with if possible, instead of installing a new pip).
Second question: does a pip install always install with the compiler for the current python version in mind? For example, this article explained that the module numpy uses c compiled code, which has to be compiled against the same compiler of the python installation.
Thanks for the help.

You have ensurepip, good. First try
"C:\Program Files\Nuke11.1v6\python.exe" -m ensurepip
to install pip. Now do
"C:\Program Files\Nuke11.1v6\python.exe" -m pip install pathlib2
You better remove the second question and ask it in a separate topic.

Related

How to reinstall all user packages after updating Python version in Windows?

I have a Windows 7 machine running Python 3.8.5 with a very large number of physics/electronics/data analysis/simulation packages. As it turned out, I must have - for some inexplicable reason - installed the 32-bit version of Python instead of the 64-bit one despite having a 64-bit system. And I didn't notice until very recently when I was trying to install some packages that require 64-bit Python. Hence I've now downloaded and installed the latest Python version that is supported by Windows 7, which seems to be 3.8.10.
Question: What is the easiest and also fail-safe way to reinstall all the user packages - that I currently have under 3.8.5 - to 3.8.10?
For some reason, I couldn't find any "canonical" solution for this online. As it seems, Python does not come with any built-in support for updating or system migration and I'm honestly wondering why...
Anyway, my first idea was to get a list of all user (= "local"?) packages currently installed under 3.8.5, but I don't know how. Reason: Doing help('modules') inside the interpreter will list all packages and I don't see a way to "selectively apply" pip to a specific Python version, e.g. something like python-3.8.5 -m pip list --local is not supported.
After getting a list of the user packages, I was thinking to pack it into a batch command pip install package_1 package_2 <...> package_N, thus reinstalling everything to Python 3.8.10. And afterwards uninstalling Python 3.8.5 and removing all environment variables from system PATH.
Is this the proper way to do this?
Anyway, my first idea was to get a list of all user (= "local"?) packages currently installed under 3.8.5, but I don't know how.
Create a list of installed packages with pip freeze > pkglist.txt or pip list --format=freeze. If you already have one, that's great.
Then uninstall 32-bit Python 3.8.5 and clean your path for all Python related variables. Now, install 64-bit Python 3.8.10.
After reinstalling, you can install back all the packages with pip install -r pkglist.txt and it will restore the exact versions of the packages.
If you insist on having both 32-bit and 64-bit versions installed and also have the Python Launcher installed, you could invoke 32 and 64 bit versions separately with py -3.8-64 -m pip and py -3.8-32 -m pip.
I don't see a way to "selectively apply" pip to a specific Python version.
This is possible with the Python Launcher on Windows. But only between major/minor versions and not the patch versions according to its help message.
I would also recommend creating a virtual environment this time before installing the packages and leaving the root environment alone. You can create one named venv with just python -m venv venv, activate it with ./venv/Scripts/activate and proceed with the installation of packages.
Nope, doesn't work. After installing the packages with the newer Python version in PATH, e.g. Jupyter won't start.
If the Jupyter error persists, you could try pinning packages to their most recent patch/minor versions to update them and yet not break your code.
As a last resort, you could try installing Python 3.10 alongside your current Python installation (without uninstall or editing the PATH) and then installing the absolute latest versions of the packages in a 3.10 virtual environment to see if it works for you. You would invoke the two versions with Py Launcher, e.g. py -3.10 and py -3.8.
If I understood correctly, you have multiple packages like NumPy, pandas etc. installed on your machine, and you want to reinstall them "automatically" on a fresh installation of python.
The method (I use) to perform such an operation is by creating a file named setup.py which includes a list of all the packages.
Bellow, I am attaching an example of such a file I use in one of my projects:
from setuptools import setup, find_packages
setup(
name='surface_quality_tools',
version='0.1',
install_requires=["matplotlib", "psutil", "numpy", "scipy", "pandas", "trimesh", "pyglet", "networkx", "protobuf",
"numpy-stl", "sklearn", "opencv-python", "seaborn", "scikit-image", "flask", "tqdm", "pytest"],
package_data={'': ['*.json']},
packages=find_packages(include=[])
)
to run the installation you should open a command prompt from inside the project directory and run:
pip install -e .
You can find a nice example in this blog page
One common way of handling packages in Python is via virtual environments. You can use Anaconda (conda), venv or any of several other solutions. For example, see this post:
https://towardsdatascience.com/virtual-environments-104c62d48c54#:~:text=A%20virtual%20environment%20is%20a,a%20system%2Dwide%20Python).
The way this works in by keeping the Python interpreter separate from the virtual environment that contains all the necessary packages.
Probably the main reason Python doesn't feature migration tools (at least as part of standard library) is because pip - the main package tool - doesn't handle conflict resolution all too well. When you update a version of Python it might so happen (especially with niche packages) that some of them won't work any more and pip often won't be able to solve the dependencies. This is why it's a good idea to keep a separate venv for different Python versions and different projects.
The other tool you could use for easy migration is Docker which is a semi-virtual machine working on top of your host OS and containing usually some linux distribution, Python along with the necessary packages necessary for running and development.
It takes a bit of time to set up a container image initially but afterwards setting everythin on a new machine or in the cloud becomes a breeze.
Listing currently installed packages is done via pip freeze command, the output of which you can then pipe into a file to keep a record of project requirements, for example pip freeze > requirements.txt.

Python not recognizing newly installed or some already satisfied modules

I have noticed that when I install new modules using pip install example, it installs correctly but when I try importing it in a script it says: ModuleNotFoundError: No module named 'example'. To get the modules working I have to copy the module's folder to the directory of my script. Before this started happening I had installed some modules which also work now so I'm a bit confused.
I'm guessing that there's something wrong with some PATH but I'm not really sure. How can I fix this?
Edit: I'm almost 100% sure that these problems started happening when I installed Anaconda. I have made sure Anaconda's folder is added to PATH but I still have the same issue.
With Anaconda, you would benefit from installing new packages by conda install example instead of using pip.
However, it is likely that you have installed another copy of Python that comes with Anaconda, and you should check which Python you are using by typing python -V in your command prompt or checking where it is installed (for Windows machines, check here). You might have installed the package to use with one version of Python, but your system directed you to the other one.
I have uninstalled my standalone Python distribution after installing Anaconda, so my default version of Python is the version that comes with Anaconda.

Unresolved import

I get this error for every single package that I have downloaded from pip whenever I type import [package] or any variation there off. This also means that the packages do not work. This is strange, since it used to work.
Edit: Apparently it works just fine on Jupyter Notebooks, but not in a normal python file.
it seems that you are installing the module in a wrong way, specifically, to improper directory (or python version).
you can alway check if module/package is installed or not by this:
pip list
if you can see the module in list, then it is installed, if not, then you need to install it properly.
sometimes you may have different versions of python installed, you install the package to version X and try to run it on version Z. all you need to do is to clarify your python version, check if the pip is referring to the python version by doing this:
pip -V
it shows the pip version along with the python version it is referring to.
The BEST WAY
always try to create a virtual environment and install packages there.
creating a virtual environment
python -m venv <VENV_NAME>
then activate it:
source <VENV_NAME>/bin/activate # MacOS & Linux
<VENV_NAME>/Scripts/activate # Windows
then, install your packages. there shouldn't be any problems after that.

python versions, setup.py, python alias, nose

I've seen quite a few posts on this topic, but there seems to be little agreement or definitive answer. I understand that OSX (in my case 10.10.5) comes with Python2.7 installed, as detailed in the official python docs.
After following those instructions, I now have python 2.7 and python 3.4 installed (accessible by $ python and $ python3, respectively) and perhaps more versions. I find python versions in a few places:
/Applications/Python 3.4
/usr/local/bin (contains python, python2, python2.7, python3, python3.4
/usr//bin (contains python2.6, python2.7)
/System/Library/Frameworks/Python.framework/Versions (contains 2.3, 2.5, 2.6, 2.7, Current)
Moreover, I'm still having trouble when I install python packages...and I think it's because I don't completely understand where certain packages are installed, their PATH variables and how they are aliased.
Problem:
For example, after successfully installing nose with pip install nose, I cannot use $ nosetests because the command is not found.
Suggested solutions:
Inspect the nose directories and look for nosetests to alias it (but despite there being many other files within /nose, regardless of the version, there isn't anything about nosetests).
Another suggestion is to run python setup.py install within /nose but that simply gives this error:
/usr/local/Cellar/python/2.7.10_1/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'setup.py': [Errno 2] No such file or directory
Some posts suggested simply using sudo as a fix to the problem...but I have been warned against using sudo without understanding why I need it, and the nose docs don't mention needing sudo, so I don't want to simply put a band-aid over a deeper issue that may arise later.
My original question was basically two questions: one about conflicting versions of python and the other about installing the nose package specifically.
For the first question, I found jonrsharpe's initial comment to my question very helpful. He links to a tutorial that explains virtualenv and how to set it up. However, I was still uncertain how to install packages only within that virtualenv, because even when I'm in the virtualenv directory and I pip install, the package is still listed under pip freeze when outside of that virtualenv directory.
Solution:
This tutorial was helpful in further explaining virtualenv, what it is and how to use it. In short, virtualenv creates a copy of python in the designated virtualenv directory that has its OWN pip. That local pip must be used to install packages locally for that virtualenv, referencing it as [virtualenv_dir_you_created]/bin/pip install nose
And, by using virtualenv, problems with permission for writing to packages globally disappear.

How to install pymorph on windows?

I am very new to python so sorry if this is a trivial question. I am developing on windows and I want to install the following library pymorph
I know it can be installed on linux but how to install it on windows enviroment ?
Thanks
Download the package. Extract the package, cd to the folder (make sure setup.py is in the folder) and run python setup.py install from your cmd while in said folder.
There are 2 main ways to install a python dependency.
First method
You can install pymorph (and almost all python dependencies) with the easy_install software. You can retrieve it here.
Example :
Run the setuptools-0.6c11.win32-py2.7.exe if you are using python version 2.7.
After that, you have the easy_install executable.
To install pymorph (in a shell, using cmd.exe) :
easy_install-2.7 pymorph
Don't forget to adapt the name of the easy_install executable with the good python version.
The advantage of this method is that it uses the Python Package Index :
According to Wikipedia :
The Python Package Index or PyPI is the official third-party software repository for the Python programming language. Python developers intend of it to be a comprehensive catalog of all open source Python packages.1 It is analogous to CPAN, the repository for Perl. Package managers such as EasyInstall, pip and PyPM use PyPI as the default source for packages and their dependencies.
Second method
In a shell, using the cmd.exe, go to the pymorph folder. After that, always in your shell :
python setup.py install
In a same way, python executable name can depend of the version of python. Example :
python2.7 for python version 2.7

Categories