5

I'm very new in python, sorry if my question is very basic.I have a shell script that using it to run a .py file on a cluster. Here is my shell script:

    #!/bin/bash
    module add python/2.6
    python Myfile.py 

Python has been installed on the cluster but some of the libraries and packages needs to be installed. For example, I need to install Numpy package, is there any way that I can do it inside my shell script or my .py file before "import" it?

Thanks

5
  • You may try pip Commented Sep 9, 2014 at 1:37
  • As @skyuuka said, pip will achieve exactly what you're asking. Check out virtualenv while you're at it. Commented Sep 9, 2014 at 2:11
  • You can also consider setuptools (pypi.python.org/pypi/setuptools). After you install it, just run easy_install Numpy in your shell script. Commented Sep 9, 2014 at 2:20
  • Actually I do not have root access, so I can not use "sudo" :( Commented Sep 9, 2014 at 2:36
  • 1
    I would suspect most clusters would have numpy available, maybe through a tool like softenv. Maybe talk to the cluster admin about it? Commented Sep 9, 2014 at 2:44

1 Answer 1

5

For this (and similar) use case, I would recommend a combination of pip and virtualenv.

You would install pip into your system Python install (i.e. sudo apt-get install python-pip), and then install virtualenv via pip, i.e. pip install virtualenv).

You can then create a specific virtualenv for this project. This represents a sandboxed environment with specific versions of libraries that are specified traditionally through a requirements file (using the -r option), but can also be specified individually through the command line.

You would do this via command like virtualenv venv_test, which will create a virtualenv directory named venv_test in the current directory. You can then run pip from that virtualenv's bin dir to install packages.

For example, to install the flask package in that virutalenv, you would run:

venv_test/bin/pip install flask

You can then either run source venv_test/bin/activate to put the current shell into the virtualenv's, or invoke a script directly from the virtualenv's interpreter, i.e.:

venv_test/bin/python foo.py

Here's link to a virtualenv introduction for some additional details/steps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.