0

I'm writing a script that uses MySQLdb and pymssql modules. I'm using python 2.7. I need to run this script on a computer (Linux) on which I have no permissions to install the modules or to add to the path variable.

I was thinking of using the import from syntax, but it lookes like it's looking for a .py file that doesn't exist for any of the two modules.

I'm getting the error: 'No module named pymssql' for the line 'from pymssql import pymssql' or 'from pymssql import *'. (pymssql is a directory containing the files in the pymssql source zip).

Edit: The script will run on multiple hosts so I'm trying to avoid installations of any kind.

1
  • The script will be running automatically on a different computers (same distribution and same python version installed, but different hosts) so I need to make this script as independent as possible. It can be deployed with additional files. Commented Sep 3, 2015 at 9:14

2 Answers 2

1

You can try to copy all your dependencies into a special folder, your own app-packages or so, and then at the beginning of your programme, you would add the path of each dependency into the sys.path so that imports would work. Of course, when you distribute your code, you would have to include a "hard-copy" of all packages you need, i.e. the app-packages.

Something like this:

# At the beginning of your programme
import sys
sys.path.extend([all the paths of your packages inside of app-packages])

This should not be hard to automate. You could scan all the elements in the app-packages and add them to the path. Optionally with filtering for .egg, .zip, etc extensions. Some not tested code for this could be:

import os
import sys
dep_folder = 'app-packages'  # asuming your main script is placed where app-packages is
dependencies = [os.path.join(dep_folder, dep) for dep in os.listdir(dep_folder)]  # optionally you can filter here

sys.path.extend(dependencies)

This way, when later on the import pymssql is hit, the import mechanism would also look in the paths that you added.

Another option is to use a virtual environment. There you would not have any problem to install your dependencies via pip, but I expect that not to be easy to distribute.

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

11 Comments

I was thinking about that, but the script will run on some of the hosts with users that have no permissions to change the env variables...
You can always run a script that does not set or change environment variables, by prepending PYTHONPATH on the command line: PYTHONPATH=<whatever> python <some-script.py>
@JohnEllis, changing sys.path does not require any permission nor changes any environment variable. It is just an object in the heap of the process. When the process is finished, those changes don't exist anymore.
@Evert, the problem with that is that you have to create a batch file, and in this batch you have to define the path of each egg that you have added. I think it is way better to update the sys.path on runtime.
@ikaros45 I added the paths, I printed the sys.path to make sure it was added and I'm getting "No module named pymssql". I unzipped the pymssql-2.1.1.tat.gz to this path and added the init.py. Am I missing something?
|
1

You can often install packages locally using

$ python setup.py install --user

This doesn't require installation permissions.

The alternative is to set (or add to) your PYTHONPATH. In bash:

$ export PYTHONPATH=${PYTHONPATH}:/path/to/package-base

The package "base" directory is often the subdirectory named after the package, while e.g. setup.py is generally found one directory up.

If you don't like changing your environment, you can make it a one-off:

$ PYTHONPATH=${PYTHONPATH}:/path/to/package-base python /some/script.py

You could possibly wrap this in an alias that includes the lengthy first part.


Finally, you could also try and create a Python virtual environment. For details on that, please see the linked documentation, or search around.

3 Comments

The problem with the local installation of packages is that it is very unportable... once I was in a similar situation and I really had problems getting my programme's dependencies in my peers machines.
I'm trying to avoid any lical installation because I have no control over which computer will run the script. like ikaros45 said.
The local installation is a user specific installation; so yes, it will not work for other users using your installation. But the same goes in a sense for a virtual environment. PYTHONPATH would then be your solution.

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.