I am interested in a reliable and robust procedure for collecting python package and dependencies for the Psychopy library into a single collection or environment to make a self-contained and maintainable installation. As well it would be good to have some general recommendations on the recommended way to do this since googling Nixos and Python yields a number of approaches, some of which use poorly documented function e.g. myEnvFun
Psychopy is a python package used for psychology experiments. It has several dependencies, most of which are python packages, but not all (e.g. AVbin); and most of which are in the nixos package collection, but not all (pyo and py-parallel).
My goal would be to be able to get all the needed pieces together and have a functioning psychopy environment with a single installation request. I have figured out how to get psychopy installed, but the path's don't play nicely.
For example if the following is saved as ~/pkg/psychopy/default.nix
let
pkgs = import <nixpkgs> {};
in
{stdenv ? pkgs.stdenv, python ? pkgs.python, fetchurl ? pkgs.fetchurl}:
with pkgs;
buildPythonPackage {
name = "psychopy";
src = fetchurl {
url = http://sourceforge.net/projects/psychpy/files/PsychoPy/PsychoPy-1.82.02.zip;
md5 = "52309280bdca4408970aab0952c674e4";
};
buildInputs = [
python27
];
}
One can run nix-env -f ~/pkg/ -iA psychopy and Psychopy will be installed, but it will not be easily useable because the path to the psychopy library is not seen by any system wide python2 installation, or even the python version that is installed as part of the build inputs.
This leads to the following questions that though they are specifically about psychopy would apply more generally to python and Nixos.
Is the recommend practice to install python packages that exist in the nixos expression collections (e.g. numpy and scipy) once as system or user wide packages or with each particular experimental library?
If one wishes to bundle together a python collection with more than one library outside the nixos expression channel (e.g. psychopy and pyo and pyparallel) what is the recommended procedure? And how does this change if some non-python software is required, e.g. in this case AVbin (which actually has installation instructions that refers to paths that are not standard in Nixos to my understanding, i.e. /usr/lib)?
Can some discussion of handling paths in Python with the context of Nixos be shared?
myEnvFun is deprecated, where did you read about it?
Answers:
It's recommended to create environments with nix-shell, you should be able to run it inside ~/pkg/psychopy/ and get $PYTHONPATH populated.
The idea behind Nix is not to have any global sets of packages, but rather environments for each need.
By just declaring AVBin as build dependency it should be enough. Note that your users will need to install Nix. If you want to avoid that, you'll need to write a wrapper that will do something similar to nix-shell.
There isn't much going on here. All Nix packages are build in isolated chroots. Nix has a concept called setup hooks, which are executed for each package in the dependency tree. So for Python packages https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/2.7/setup-hook.sh#L15 is called in order to populate $PYTHONPATH. For command line programs we then wrap the resulting script with $PYTHONPATH hardcoded.
For discussion it's best to join Freenode IRC channel #nixos
Related
I'm trying to write a plugin for Sublime Text 3.
I have to use several third party packages in my code. I have managed to get the code working by manually copying the packages into /home/user/.config/sublime-text-3/Packages/User/, then I used relative imports to get to the needed code. How would I distribute the plugin to the end users? Telling them to copy the needed dependencies to the appropriate location is certainly not the way to go. How are 3rd party modules supposed to be used properly with Sublime Text plugins? I can't find any documentation online; all I see is the recommendation to put the modules in the folder.
Sublime uses it's own embedded Python interpreter (currently Python 3.3.6 although the next version will also support Python 3.8 as well) and as such it will completely ignore any version of Python that you may or may not have installed on your system, as well as any libraries that are installed for that version.
For that reason, if you want to use external modules (hereafter dependencies) you need to do extra work. There are a variety of ways to accomplish this, each with their own set of pros and cons.
The following lists the various ways that you can achieve this; all of them require a bit of an understanding about how modules work in Python in order to understand what's going on. By and large except for the paths involved there's nothing too "Sublime Text" about the mechanisms at play.
NOTE: The below is accurate as of the time of this answer. However there are plans for Package Control to change how it works with dependencies that are forthcoming that may change some aspect of this.
This is related to the upcoming version of Sublime supporting multiple versions of Python (and the manner in which it supports them) which the current Package Control mechanism does not support.
It's unclear at the moment if the change will bring a new way to specify dependencies or if only the inner workings of how the dependencies are installed will change. The existing mechanism may remain in place regardless just for backwards compatibility, however.
All roads to accessing a Python dependency from a Sublime plugin involve putting the code for it in a place where the Python interpreter is going to look for it. This is similar to how standard Python would do things, except that locations that are checked are contained within the area that Sublime uses to store your configuration (referred to as the Data directory) and instead of a standalone Python interpreter, Python is running in the plugin host.
Populate the library into the Lib folder
Since version 3.0 (build 3143), Sublime will create a folder named Lib in the data directory and inside of it a directory based on the name of the Python version. If you use Preferences > Browse Packages and go up one folder level, you'll see Lib, and inside of it a folder named e.g. python3.3 (or if you're using a newer build, python33 and python38).
Those directories are directly on the Python sys.path by default, so anything placed inside of them will be immediately available to any plugin just as a normal Python library (or any of those built in) would be. You could consider these folders to be something akin to the site-packages folder in standard Python.
So, any method by which you could install a standard Python library can be used so long as the result is files ending up in this folder. You could for example install a library via pip and then manually copy the files to that location from site-packages, manually install from sources, etc.
Lib/python3.3/
|-- librarya
| `-- file1.py
|-- libraryb
| `-- file2.py
`-- singlefile.py
Version restrictions apply here; the dependency that you want to use must support the version of Python that Sublime is using, or it won't work. This is particularly important for Python libraries with a native component (e.g. a .dll, .so or .dylib), which may require hand compiling the code.
This method is not automatic; you would need to do it to use your package locally, and anyone that wants to use your package would also need to do it as well. Since Sublime is currently using an older version of Python, it can be problematic to obtain a correct version of libraries as well.
In the future, Package Control will install dependencies in this location (Will added the folder specifically for this purpose during the run up to version 3.0), but as of the time I'm writing this answer that is not currently the case.
Vendor your dependencies directly inside of your own package
The Packages folder is on the sys.path by default as well; this is how Sublime finds and loads packages. This is true of both the physical Packages folder, as well as the "virtual" packages folder that contains the contents of sublime-package files.
For example, one can access the class that provides the exec command via:
from Default.exec import ExecCommand
This will work even though the exec.py file is actually stored in Default.sublime-package in the Sublime text install folder and not physically present in the Packages folder.
As a result of this, you can vendor any dependencies that you require directly inside of your own package. Here this could be the User package or any other package that you're creating.
It's important to note that Sublime will treat any Python file in the top level of a package as a plugin and try to load it as one. Hence it's important that if you go this route you create a sub-folder in your package and put the library in there.
MyPackage/
|-- alibrary
| `-- code.py
`-- my_plugin.py
With this structure, you can access the module directly:
import MyPackage.alibrary
from MyPackage.alibrary import someSymbol
Not all Python modules lend themselves to this method directly without modification; some code changes in the dependency may be required in order to allow different parts of the library to see other parts of itself, for example if it doesn't use relative import to get at sibling files. License restrictions may also get in the way of this as well, depending on the library that you're using.
On the other hand, this directly locks the version of the library that you're using to exactly the version that you tested with, which ensures that you won't be in for any undue surprises further on down the line.
Using this method, anything you do to distribute your package will automatically also distribute the vendored library that's contained inside. So if you're distributing by Package Control, you don't need to do anything special and it will Just Work™.
Modify the sys.path to point to a custom location
The Python that's embedded into Sublime is still standard Python, so if desired you can manually manipulate the sys.path that describes what folders to look for packages in so that it will look in a place of your choosing in addition to the standard locations that Sublime sets up automatically.
This is generally not a good idea since if done incorrectly things can go pear shaped quickly. It also still requires you to manually install libraries somewhere yourself first, and in that case you're better off using the Lib folder as outlined above, which is already on the sys.path.
I would consider this method an advanced solution and one you might use for testing purposes during development but otherwise not something that would be user facing. If you plan to distribute your package via Package Control, the review of your package would likely kick back a manipulation of the sys.path with a request to use another method.
Use Package Control's Dependency system (and the dependency exists)
Package control contains a dependency mechanism that uses a combination of the two prior methods to provide a way to install a dependency automatically. There is a list of available dependencies as well, though the list may not be complete.
If the dependency that you're interested in using is already available, you're good to go. There are two different ways to go about declaring that you need one or more dependencies on your package.
NOTE: Package Control doesn't currently support dependencies of dependencies; if a dependency requires that another library also be installed, you need to explicitly mention them both yourself.
The first involves adding a dependencies key to the entry for your package in the package control channel file. This is a step that you'd take at the point where you're adding your package to Package Control, which is something that's outside the scope of this answer.
While you're developing your package (or if you decide that you don't want to distribute your package via Package Control when you're done), then you can instead add a dependencies.json file into the root of your package (an example dependencies.json file is available to illustrate this).
Once you do that, you can choose Package Control: Satisfy Dependencies from the command Palette to have Package Control download and install the dependency for you (if needed).
This step is automatic if your package is being distributed and installed by Package Control; otherwise you need to tell your users to take this step once they install the package.
Use Package Control's Dependency system (but the dependency does not exist)
The method that Package Control uses to install dependencies is, as outlined at the top of the question subject to change at some point in the (possibly near) future. This may affect the instructions here. The overall mechanism may remain the same as far as setup is concerned, with only the locations of the installation changing, but that remains to be seen currently.
Package Control installs dependencies via a special combination of vendoring and also manipulation of the sys.path to allow things to be found. In order to do so, it requires that you lay out your dependency in a particular way and provide some extra metadata as well.
The layout for the package that contains the dependency when you're building it would have a structure similar to the following:
Packages/my_dependency/
├── .sublime-dependency
└── prefix
└── my_dependency
└── file.py
Package Control installs a dependency as a Package, and since Sublime treats every Python file in the root of a package as a plugin, the code for the dependency is not kept in the top level of the package. As seen above, the actual content of the dependency is stored inside of the folder labeled as prefix above (more on that in a second).
When the dependency is installed, Package Control adds an entry to it's special 0_package_control_loader package that causes the prefix folder to be added to the sys.path, which makes everything inside of it available to import statements as normal. This is why there's an inherent duplication of the name of the library (my_dependency in this example).
Regarding the prefix folder, this is not actually named that and instead has a special name that determines what combination of Sublime Text version, platform and architecture the dependency is available on (important for libraries that contain binaries, for example).
The name of the prefix folder actually follows the form {st_version}_{os}_{arch}, {st_version}_{os}, {st_version} or all. {st_version} can be st2 or st3, {os} can be windows, linux or osx and {arch} can be x32 or x64.
Thus you could say that your dependency supports only st3, st3_linux, st3_windows_x64 or any combination thereof. For something with native code you may specify several different versions by having multiple folders, though commonly all is used when the dependency contains pure Python code that will work regardless of the Sublime version, OS or architecture.
In this example, if we assume that the prefix folder is named all because my_dependency is pure Python, then the result of installing this dependency would be that Packages/my_dependency/all would be added to the sys.path, meaning that if you import my_dependency you're getting the code from inside of that folder.
During development (or if you don't want to distribute your dependency via Package Control), you create a .sublime-dependency file in the root of the package as shown above. This should be a text file with a single line that contains a 2 digit number (e.g. 01 or 50). This controls in what order each installed dependency will get added to the sys.path. You'd typically pick a lower number if your dependency has no other dependencies and a higher value if it does (so that it gets injected after those).
Once you have the initial dependency laid out in the correct format in the Packages folder, you would use the command Package Control: Install Local Dependency from the Command Palette, and then select the name of your dependency.
This causes Package Control to "install" the dependency (i.e. update the 0_package_control_loader package) to make the dependency active. This step would normally be taken by Package Control automatically when it installs a dependency for the first time, so if you are also manually distributing your dependency you need to provide instructions to take this step.
The python package metadata specification includes the field Provides-Dist, which seems to allow for a package to declare that it provides the functionality of a different package (or virtual package).
This feature seems like it would be useful for something like TensorFlow where the same interface is provided by many different related packages: tensorflow, tensorflow-gpu, tf-nightly, ..., making it effectively impossible for other python packages to "install_requires" tensorflow (most major packages omit it).
According to that specification document, this feature is "rarely used".
Are there python packaging tools that use Provides-Dist? Either generating it or using it?
Is there any other documented reason (including public discussions) why this feature is not supported? The metadata specification vaguely declares "it isn’t at all clear how tools should interpret them in the context of an open index server", but it's not clear to me why that is a problem since presumably any abusive package would have to be explicitly installed by the user first, and there are plenty of other ways in which installed packages can be evil.
As far as I know no tool supports Provides-Dist.
The following comment seems to corroborate:
https://discuss.python.org/t/conditional-package-install-depending-on-other-packages-in-environment/4140/2
I have a mainly c++ project that I use CMake to manage. After setting cmake_install_prefix and configuring, it generates makefiles which can then be used to build and install with the very standard:
make
make install
At this point, my binaries end up in cmake_install_prefix, and they can be executed with no additional work. Recently I've added some Python scripts to a few places in the source tree, and some of them depend on others. I can use CMake to copy the Python files+directory structure to the cmake_install_prefix, but if I go into that path and try to use one of the scripts, Python cannot find the other scripts used as imports because PYTHONPATH does not contain cmake_install_prefix. I know you can set an environment variable with CMake, but it doesn't persist across shells, so it's not really "setup" for the user for more than the current terminal session.
The solution seems to be to add a step to your software build instructions that says "set your PYTHONPATH". Is there any way to avoid this? Is this the standard practice for "installing" Python scripts as part of a bigger project? It seems to really complicate things like setting up continuous integration for the project, as something like Jenkins has to be manually configured to inject environment variables, whereas nothing special was required for it to build and execute executables built from c++ code.
Python provides sys.path list, which is used for search modules with import directives. You may adjust this list before include your modules:
script1.py:
# Do some things useful for other scripts
script2.py.in:
# Uses script1.py.
...
sys.path.insert(1, "#SCRIPT1_INSTALL_PATH#")
import script1
...
CMakeLists.txt:
...
# Installation path for script1. Depends from CMAKE_INSTALL_PREFIX.
set(SCRIPT1_INSTALL_PATH ${CMAKE_INSTALL_PREFIX}/<...>)
install(FILES script1.py DESTINATION ${SCRIPT1_INSTALL_PATH}
# Configure 'sys.path' in script2.py, so it may find script1.py.
configure_file("script2.py.in" "script2.py" #ONLY)
set(SCRIPT2_INSTALL_PATH ${CMAKE_INSTALL_PREFIX}/<...>)
install(FILES script2.py DESTINATION ${SCRIPT2_INSTALL_PATH}
...
If you want script2.py to work both in build tree and in install tree, you need to have two instances of it, one which works in build tree, and one which works after being installed. Both instances may be configured from single .in file.
In case of compiled executables and libraries, similar mechanism is uses for help binaries to find libraries in non-standard locations. It is known as RPATH.
Because CMake
knows every binary created (it tracks add_executable and add_library calls),
knows linkage between binaries (target_link_libraries call is also tracked),
has full control over linking procedure,
CMake is able to automatically adjust RPATH when install binaries.
In case of Python scripts CMake doesn't have such information, so adjusting linkage path should be performed manually.
I was looking for something similar to perl's Dumper functionality in python. So after googling I found one which serves me well # https://gist.github.com/1071857#file_dumper.pyamazon
So I downloaded and installed it and it works fine.
But then I came accross PyPI: http://pypi.python.org/pypi which looks like CPAN equivalent for python.
So I searched for the Dumper module there and I could not find it there. I was hoping that this seems like a very basic module and should have been listed in PyPI.
So my question is, if I have to install a python module, then should I search in PyPI first and if i do not find then look other places on google?
OR is there any other Python Module repository apart from PyPI?
I am learning python and hence this question.
thanks.
If you are using pip, pip search package_name would help you do the same as searching on the web interface provided by PyPi.
Once located, installing a python package is of course as easy as
pip install package_name
Some python libraries may be in development stage and may not directly be available on PyPi OR you may want a specific commit has (git) of that library and if you can find that library's source on github.com or on bitbucket.com for example, you can do
pip install -e git+git://github.com/the/repo/url.git#egg=package_name
And regarding your question about perl Dumper, perl's Dumper has two main uses iirc -
data persistence
debugging and inspecting objects.
As far as I know, there's no exact equivalent of perl's Dumper in python.
However, I use pickle for data persistence.
And pprint is useful for visually inspecting objects/debug.
Both of which are standard, built-in modules in Python. There's no necessity for 3rd party libraries for these functionalities.
If you want to use what is here - https://gist.github.com/1071857#file_dumper.pyamazon.
What you need to do is to copy the code and place it in a local file in your project directory. You can name the file something like pydumper.py. Or any name you prefer really, but end it with suffix .py.
In your project, you can import the functions and classes defined in pydumper.py by doing
from pydumper import *
or if you want to be specific (which is preferred. it's better to be explicit about what you are importing.)
from pydumper import Dumper
and you can start using the Dumper class in your own code.
Are you looking for something like easy_install from setuptools? I might have misunderstood your question as I don't use perl.
From the Scripts directory in the python installation directory ("c:/python27/Scripts" on my machine), you can install modules from the command line like so:
easy_install modulename
Makes life alot easier if you set the Scripts directory to your PATH variable.
I'm attempting to build Python 2.6.2 from source on my Linux system. It has ncurses installed on /usr/local/, and curses.h is on /usr/local/include/ncurses. So curses.h isn't found on the include path, and those packages fail in the Python build.
What's the right solution to this? Is Python supposed to include <ncurses/curses.h>? Should /usr/local/include/ncurses be in the include path? Should there be a link from the files in the ncurses directory to /usr/local/include?
Or is there some simpler solution?
With many Open Source packages, you can set:
export CPPFLAGS="-I/usr/local/include"
or even:
export CPPFLAGS="-I/usr/local/include/ncurses"
before running the configure script. I haven't compiled Python recently enough to be sure that works, but it probably does -- I have ncurses installed under /usr/gnu (because /usr/local/ is automounted and contains antiques) and I don't remember having to use anything special to get it to work.
Double-checked...
The configure script only includes <curses.h>. I had to use:
export CPPFLAGS="-I/usr/gnu/include -I/usr/gnu/include/ncurses"
export LDFLAGS="-L/usr/gnu/lib"
./configure
To get the Python (2.5) configure to accept curses. You'd replace 'gnu' with 'local' for your configuration.
I know this is a very old question, but the problem still occurred to me when compiling python 3.6.0 from source so I guess it's still relevant.
Recent versions of ncurses come in several flavors: normal, wide character support, threaded. In order to allow programmers to keep and use different flavors, besides naming the libraries differently (ncursesw.so, ncursest.so, etc), the ncurses configure script sets up the makefile to put the header files in subdirectories by default. This also allows to have different curses implementations alongside ncurses, as specified in the man page.
Some programs, however, still assume that curses.h, along all other ncurses headers, are placed in the top level include search paths, and won't look into subdirectories. In many linux distributions there is usually some sort of workaround for the problem in ncurses developement packages, but if you are compiling ncurses from source, there are two possible approaches to solve the issue:
Using CPPFLAGS or equivalent, as the accepted answer suggests. It works, but you have to set the appropriate compilation flags every time.
Configuring ncurses with --enable-overwrite. This will install the ncurses header files in the top level include directory, according to your --prefix.
If you don't plan to install an alternate curses library, it's completely safe to put the ncurses headers in the top level include path, and it is the approach followed by Gentoo.