Reputation: 31
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.
Upvotes: 0
Views: 3818
Reputation:
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.
Upvotes: 1
Reputation: 15145
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.
Upvotes: 1