editing code of a python module - python

Running pymatlab on my machine results in
Exception AttributeError: "'MatlabSession' object has no attribute 'engine'" in > ignored
after the command session = pymatlab.session_factory() is run.
How to fix this problem has been discussed already here:
Running MATLAB from Python
It looks like one line of code in the sessionfactory.py script in the pymatlab module has to be changed in a minor way. The problem I have is that the pymatlab module which is installed on my machine is in .egg form and it doesn't look like it is possible to change the code directly with a text editor. Any suggestions on how to do that?
Thanks

If you use easy_install, check
How do I forbid easy_install from zipping eggs?
If you prefer pip (and you probably should), check
pip: “Editable” Installs, i.e.
pip install -e pymatlab

Related

Trying to import GitHub module

I'm trying to import https://github.com/chrisconlan/algorithmic-trading-with-python in my code. I've never imported anything from GitHub before and have looked at various other questions that have been asked on Stack Overflow regarding this problem but it just doesn't work. When I try to run the 'portfolio.py' code for example I keep getting a ModuleNotFound error for 'pypm'. What exactly is the correct way to import such a module or the whole GitHub directory?
I'm working with Visual Studio Code on Windows.
You will need to pip install the module. In your case the command you would need to run is python -m pip install -U git+https://github.com/chrisconlan/algorithmic-trading-with-python. Once you have done that you need to find the name of the module. You can do this with pip list. Find the name of the module you just installed.
Then you just stick import <module name> at the top of your code with the rest of your imports.
What i used to do in this is to clone the repository on the folder where are installed the python's packages. This is useful when you do not want to use the pip cmd tool, keeping the pip's cache memory under control.

How can I find out, which dependency is missing for a pip package?

I have installed a pip package, but trying to use it I get errors about some modules or classes not being available. How can I investigate the root cause myself?
Motivation
On OpenSuse Leap 15.1, no binary package for AutoKey is available. As a consequence I was trying to install it with
pip install --user autokey # --user because I'm not root at work.
After executing this, when I try to run the GUIs, I get import-related exceptions:
For autokey-gtk: ValueError: Namespace AppIndicator3 not available, raised by gi.require_version(...). [1]
For qutokey-qt: ImportError: cannot import name 'Qsci', raised by an from ... import Qsci line.
While trying to figure out how to resolve the error, likely related to missing dependencies, I started to wonder: Is there any way to figure out what is missing from the error message? Running pip3 search Qsci and pip3 search AppIndicator3 would seem an obvious solution, but don't yield any results.
I intentionally omit the full backtrace for now in order to avoid distracting from the core question: How can I try to find the solution myself?
Documentation of package you've installed should have instructions about any external dependencies on various platforms.
Second option is to use pip_missing_reqs.
I recognize this error as specific to the way PyGObject handles dependencies. You should see a line similar to the following in the stack trace:
File "/path/to/blah/indicator.py", line 31, in <module>
gi.require_version('AppIndicator3', '0.1')
Once you install the appropriate GLib/Gtk feature, PyGObject will be able to find it via GObject introspection, and automatically bind to it, so there's no Python-specific package that needs to be installed to access the app indicator feature.
On my Ubuntu 18.04 system, installing these packages was enough:
$ sudo apt install gir1.2-appindicator3-0.1 libappindicator3-0.1-cil
Here are details about the two packages:
https://packages.ubuntu.com/bionic/gir1.2-appindicator3-0.1
https://packages.ubuntu.com/bionic/libappindicator3-0.1-cil
I'm not convinced that the second one was necessary.

Can't import module after manually installing

I have installed the module pynauty via https://web.cs.dal.ca/~peter/software/pynauty/html/install.html. Everything seemed to work, and I believe that it is fully installed. However, when I try importing it in python shell, it says there is no module named pynauty. I think I am missing a final step in installing. Does anyone know what that final step would be?
I just grappled with this problem myself. As creimers pointed out, the setup script calls pip install --user . which installs pynauty in ~/.local/lib/pythonX.X/site-packages.
What you probably want is to have it installed in /usr/bin/python (or whatever pops up when you type which python). Assuming you already called make user-ins, all you need to do is run
sudo pip install .
from inside where you extracted and built your pynauty.

How to install a package using the python-apt API

I'm quite a newbie when it comes to Python, thus I beg foregiveness beforehand :). That said, I'm trying to make a script that, among other things, installs some Linux packages. First I tried to use subopen as explained here. While this can eventually work, I stumbled upon the python-apt API and since I'm not a big fan or re-inventing the wheel, I decided to give a try.
Problem comes when trying to find examples/tutorials on installing a package using python-apt. Searching the documentation I found the PackageManager class that has some methods to install a package. I tried some simple code to get this working:
apt_pkg.PackageManager.install("python")
This does not seem to work that easily, the install method expects apt_pkg.PackageManager instead of a plain String. Thus, looking a bit more, I found this example that looks promising, but I'm a bit reluctant to use since I don't really understand some of what is happening there.
Then, has anyone tried to install a package using python-apt or should I go for using plain-old subopen style?
Thanks!
It's recommended to use the apt module from the python-apt Debian package. This is a higher level wrapper around the underlying C/C++ libapt-xxx libraries and has a Pythonic interface.
Here's an example script which will install the libjs-yui-doc package:
#!/usr/bin/env python
# aptinstall.py
import apt
import sys
pkg_name = "libjs-yui-doc"
cache = apt.cache.Cache()
cache.update()
cache.open()
pkg = cache[pkg_name]
if pkg.is_installed:
print "{pkg_name} already installed".format(pkg_name=pkg_name)
else:
pkg.mark_install()
try:
cache.commit()
except Exception, arg:
print >> sys.stderr, "Sorry, package installation failed [{err}]".format(err=str(arg))
As with the use of apt-get, this must be run with superuser privileges to access and modify the APT cache.
$ sudo ./aptinstall.py
If you're attempting a package install as part of a larger script, it's probably a good idea to only raise to root privileges for the minimal time required.
You can find a small example in the /usr/share/pyshared/apt/progress/gtk2.py:_test() function showing how to install a package using a GTK front-end.

use "pip install/uninstall" inside a python script [duplicate]

This question already has answers here:
How can I Install a Python module within code?
(12 answers)
Closed 2 years ago.
how, inside a python script can I install packages using pip?
I don't use the os.system, I want to import pip and use it.
pip.main() no longer works in pip version 10 and above. You need to use:
from pip._internal import main as pipmain
pipmain(['install', 'package-name'])
For backwards compatibility you can use:
try:
from pip import main as pipmain
except ImportError:
from pip._internal import main as pipmain
I think those answers are outdated. In fact you can do:
import pip
failed = pip.main(["install", nameOfPackage])
and insert any additional args in the list that you pass to main(). It returns 0 (failed) or 1 (success)
Jon
It's not a good idea to install packages inside the python script because it requires root rights. You should ship additional modules alongside with the script you created or check if the module is installed:
try:
import ModuleName
except ImportError:
print 'Error, Module ModuleName is required'
If you insist in installing the package using pip inside your script you'll have to look into call from the subprocess module ("os.system()" is deprecated).
There is no pip module but you could easily create one using the method above.
I used the os.system to emulate the terminal installing a pip module, (I know os.system is deprecated, but it still works and it is also the easiest way to do it), E.G I am making a Game Engine which has multiple python scripts that all use Pygame, in the startup file I use this code to install pygame onto the user's system if they don't have it:
import os
os.system('pip install pygame')
Unfortunately, I don't know how to install pip if they don't have it so this script is dependent on pip.
If you are behind a proxy, you can install a module within code as follow...
import pip
pip.main(['install', '--proxy=user:password#proxy:port', 'packagename'])
This is a comment to this post that didn't fit in the space allotted to comments.
Note that the use case of installing a package can arise inside setup.py itself. For example, generating ply parser tables and storing them to disk. These tables must be generated before setuptools.setup runs, because they have to be copied to site_packages, together with the package that is being installed.
There does exist the setup_requires option of setuptools.setup, however that does not install the packages.
So a dependency that is required both for the installation process and for the installed package will not be installed this way.
Placing such a dependency inside install_requires does not always work as expected.
Even if it worked, one would have to pass some function to setuptools.setup, to be run between installation of dependencies in setup_requires and installation of the package itself. This approach is nested, and thus against PEP 20.
So the two flat approaches that remain, are:
run setup.py twice, either automatically (preferred), or manually (by notifying the user that the tables failed to build prior to setuptools.setup.
first call pip (or some other equivalent solution), in order to install the required dependencies. Then proceed with building the tables (or whatever pre-installation task is necessary), and call setuptools.setup last.
Personally, I prefer No.2, because No.1 can be confusing to a user observing the console output during installation, unless they already know the intent of calling setuptools.setup twice.
Besides, whatever rights are needed for installation (e.g., root, if so desired), are certainly present when setup.py is run (and exactly then). So setup.py could be considered as the "canonical" use case for this type of action.

Categories