How to run a Python module from Linux console? - python

I am a beginner in Linux and I am trying to do something very basic (yet it does not seem to work).
I have installed a python3 package. I wish to run this Python package on a specific folder from my linux machine. The idea is that this package should restructure all my files in a specific format (this is the link to the package https://github.com/SIMEXP/Data2Bids ).
In the demo of the tutorial, it is mentioned that once I install the package:
pip3 install data2bids
I should just use cd on my desired folder that needs restructuring and then simply run data2bids.
After doing so, I received a command not found error.
I have tried to run the package using the python data2bids command (thinking that maybe I should mention it is a python package. I have tried to use the full path of where the package was installed in the command, yet it did not work. I even tried to install the package in the same folder as my directory that needs restructuring, with no results.
I believe that this is a very basic problem, and the solution may be very simple. Within a linux machine, how does one exactly run a python module in a specific folder?

I am also a rookie in Linux, here's what i can think of.
First, the input is:
A directory containing some files in any extension, with names containing at minimum the information of modality and patient number. A .JSON configuration file explaining how the filenames should be read.
I assume you ran the command without the configuration file.
Second, Linux has built-in python whose version is 2.x, try to type python in command line and print python version, if it's 3.x, try to query pip installed list to see if the package was installed successfully, if not, see if the installed python 3.x directory is contained in PATH.

Related

any known way to create a device-to-device portable self-contained python runtime?

I want to distribute a python program on, say, Windows and/or Mac, but I don't want to give the user the headache of ensuring there is an appropriate python runtime installed on their machine. And i don't want to interfere with their machine's configuration by, let's say, requesting root privileges and installing a system-wide python runtime on their system that suits my program specifically because it's too invasive and might cause compatibility collisions with other installed versions of the runtime.
I would much rather have a self-contained executable that could be, for example, stored on a USB flash-drive, inserted into the system, and then maybe with a stepping-stone binary executable that just invokes the device-portable runtime on a python script that I provide, I could then run the program as if it were a self-contained binary executable (with only standard-library dependencies).
A link to this binary executable could be published into main-menu program lists, docks, or desktops. And it could be invoked by shell scripts or other executed-by-proxy mechanisms. Such a no-install/self-contained python program could potentially be a first-class user-invokable application. This is what I want to achieve.
I googled around for projects that provided a device-portable/mobile python installation and so far I've only found portablepython.com. Unfortunately it says the project is discontinued and no download link for the project is provided. it listed some similar projects but they all seemed defunkt or with a very different focus.
Does anyone know of an active project that is or includes such an independent/portable/mobile/no-install distribution for python?
or is there some way i could configure python's build system to build a noinstall-friendly product?
any ideas welcome. thanks for your input!
After more searching I found that Python.org publishes its own standalone-python distribution called the embeddable zip file.
This is exactly what I was searching for. It's a basic python standalone runtime that requires relatively few megabytes of storage.
I started with this embeddable distro and then cajoled a standalone copy of pip to work with it. Problem solved.
Improving upon #oreus2020's answer, you can download the embeddable zip file from here. Then, unzip the compressed file to a folder of your choice. Go to the root of your install and find python._pth file and open it in a text editor. Remove the "#" before import site(This file is the one which manages the environment of the portable install. If you want anything to be recognized by the portable python interpreter, just throw the path in here and that's it!). If you want pip, go to this page and save it in the root of your portable install and run it using the portable python interpreter like ./python get-pip.py from a commandline opened at the root of your install. Pip installed! To use the pip, do ./python -m pip <commands> from the commandline opened at the root of your install and then open the python._pth file and insert the following below the "." ./Lib/site-packages ./Scripts. Voila, you got yourself a python portable install!
My python._pth file looks like:
python39.zip
.
# Uncomment to run site.main() automatically
./Repo
./Repo/Code
./Repo/Code/cogs
./Lib/site-packages
./Scripts
import site
If you are still wondering, here is the link to the one I made for myself.
P.S. Pardon my bad English

Portable Python Script with Module

I'm new to using python modules.
I'm currently working on a python 2.7 script that will be deployed to many remote computers (which have python 2.7 on them). The problem is that the script needs to use a module, which I am not allowed to install on those computers.
I'm wondering if it is possible to include the module files in the same package as my script (possibly have them compiled first), and then have the script import the library from that local folder, thus achieving a "portable" script.
If that is possible, how would I go about doing that?
Specifics: I'm running 2.7.11 on Windows needing to use Paramiko.
I'm asking this question because the similar questions that I can find either do not answer mine, or expect me to be familiar with core python structures with which I am not. I also DON'T want to include the entirety of python and then install the module onto that, something I see is often called Portable Python. I just want to send my script and the module and nothing more.
Many thanks!
To install modules in a specific directory, you can try pip install module --target=.
By default python search for those modules in same directory as the script first, then, if not available, it will search for python install lib files.

Changes in Python scripts are not accepted

I'm new to Python, so I think my question is very fundamental and is asked a few times before but I cannot really find something (maybe because I do not really know how to search for that problem).
I installed a module in Python (reportlab). Now I wanted to modify a python script in that module but it seems that the python interpreter does not notice the updates in the script. Ironically the import is successful although Python actually should not find that package because I deleted it before. Does Python uses something like a Cache or any other storage for the modules? How can I edit modules and use those updated scripts?
From what you are saying, you downloaded a package and installed it using either a local pip or setup.py. When you do so, it copies all the files into your python package directory. So after an install, you can delete the source folder because python is not looking here.
If you want to be able to modify, edit, something and see changes, you have to install it in editable mode. Inside the main folder do:
python setup.py develop
or
pip install -e .
This will create a symbolic link to you python package repository. You will be able to modify sources.
Careful for the changes to be effective, you have to restart your python interpreter. You cannot just import again the module or whatever else.

Why is python27.dll not part of python installed folder but in Windows system folder

As described in: http://bugs.python.org/issue22139, the python27.dll is installed in the windows systems (in my case C:\Windows\Systems32) folder.
But I would like to know why? Why is it not installed next to the python.exe, for example in C:\Python27\?
Reason I ask: I've made a mercurial hook in python that our developers need to use to check if the commit message is valid. It checks a.o. for a valid JIRA issue number. To prevent all our developers to install python themselves and install the required modules manually (a lot of work and errorprone), I zipped the python installation and asked the developers to unzip it locally. But they can't run it, because the python27.dll is missing, or worse, they already have another minor version of python installed, and the hook will fail due to the wrong python27.dll used. Confusing.
If I just add the python27.dll (the correct version) to the zip file, it all seems to work great. So, why is it not installed in that location in the first place? What is the advantage of installing it in C:\Windows\System32?
Hope someone can explain this to me!
Thanks in advance,
Tallandtree.
I use the Anaconda Python distribution from http://continuum.io. They put python27.dll into c:\anaconda right next to its python.exe. This distribution is also superior in that you can have multiple python environments with precisely the packages you need and switch between them easily (http://conda.pydata.org/docs/using/envs.html). You can also get the package list of one of your environments and distribute it to others.
I recommend this Python distribution over the one from python.org and Enthought, because of this issue.
.dlls are quite windows-specific files. I imagine you will have shared object (.so) files for LINUX/UNIX-specific Python stuff? You said your developer's couldn't run it, because they didn't have the correct DLL (i.e. the one relevant to their Python installation).
Also, the advantage of installing it to System32 is that it's in the default PATH. Additionally, if any other application is internally using Python and require access to the .dll file, and also NOT reference your Python directory, they will probably be looking for a location that "Actually" exists (I wanted to say guaranteed to exist, but......never mind). That location would be `C:/windows/Systems32'.
I found it to work just fine to put python27.dll in the Python directory (c:\Python27 or wherever). As long as it's in the PATH, it seems to work. I did this for a "relocatable" installation of Python. I can copy the installation directory to a Windows machine that has no Python installed, set the PATH to include that directory, and run Python, including all the libraries I had installed with pip install on the original machine.

Finding Tools/scripts/ subdirectory

Does anyone know where I could find this file on Ubuntu?
On my machine it's in:
/usr/share/doc/python2.7/examples/Tools/scripts/diff.py
However, it'll vary a little depending on your distrib and how up to date you are. But there's a really handy little tool called 'locate' that you can use to quickly find stuff on your machine.
locate diff.py | grep Tools
Gives me
/usr/share/doc/python2.7/examples/Tools/scripts/diff.py
/usr/share/doc/python2.7/examples/Tools/scripts/ndiff.py
/usr/share/doc/python3.1/examples/Tools/scripts/diff.py
/usr/share/doc/python3.1/examples/Tools/scripts/ndiff.py
As I have two versions of Python installed. I put it through grep, as sometimes locate can match quite a lot.
If you're sure that you have a file on your machine, but locate isn't finding it, you might need to update your database, which is done with the 'updatedb' command, as root. So, just run
sudo updatedb
and get a coffee (or two if you have a slow machine/very full drive) and then try again.
This command
$ locate "Tools/scripts/diff.py"
will find the location of the file if it's installed. This depends on the database generated regularly by the updatedb command (this usually runs as a cron job, but can also be invoked manually)
FWIW, I just checked my Ubuntu installation (10.04LTS) and didn't find it. Perhaps only Python versions 2.7+ have this (the default version that came with this install is still v2.6.5)
I needed to know this for a different environment, namely Linux running an Amazon Machine Image (AMI). I installed Python 3.5 via "yum" but couldn't find the Tools/scripts directory using find/locate, etc., or any other "yum" package which included it.
So in the end I cloned the python source tree mirror:
git clone https://github.com/python-git/python python
This downloads the Tools/scripts folder which I then moved to some standard location. I needed the "2to3" program from "scripts" and this worked. There is also a "clone or download" link at that URL where the package can be downloaded in the usual way if git is not available.
I couldn't find an easy way to install the Tools/scripts via "yum" on AMI Linux, which would still be my preference.

Categories

Resources