views:

116

answers:

5

I want to be able to run my Python project from the command line. I am referencing other projects, so I need to be able run modules in other folders.

One method of making this work would be to modify the Pythonpath environment variable, but I think this is an abuse. Another hack would be to copy all the files I want into a single directory and then run Python. Is there a better method of doing this?

Note: I am actually programming in Eclipse, but I want to be able to run the program remotely.

Similar questions:

A: 

If by "run modules" you mean importing them, you might be interested in this question I asked a while ago.

Joril
My projects don't have a unified package structure
Casebash
+1  A: 

First, I make sure that the module I want to include hasn't been installed globally. Then I add a symlink within the includee's directory:

# With pwd == module to which I want to add functionality.
ln -s /path/to/some_other_module_to_include .

and then I can do a standard import. This allows multiple versions etc. It does not require changing any global settings, and you don't need to change the program code if you work on different machines (just change the symlink).

Peter
It's irritating that symlinks aren't as flexible in windows as well.
mavnn
Again that is a solution that would work, but kind of hackish. Each project has many modules, so that would mean creating a large number of simlinks. I'm unsure, but I believe that it might not interact nicely with SVN
Casebash
You shouldn't store the symlinks in SVN. If you have multiple modules, I guess you could use a hierachy, `import parent.submodule`, and symlink only `parent`.
Peter
That would unfortunately be inconsistent with how Eclipse handles importing other projects
Casebash
+9  A: 

If you import sys, it contains a list of the directories in PYTHONPATH as sys.path

Adding directories to this list (sys.path.append("my/path")) allows you to import from those locations in the current module as normal without changing the global settings on your system.

mavnn
This is a very good solution (working for me), although I'm just curious if there is a "proper" way of doing this. Like I'd expect there to be a command line option
Casebash
To the best of my knowledge this _is_ the "proper" way. I'm curious what's wrong with it?
mavnn
Nothing is wrong with it. I just would have expected a command line option.
Casebash
You could implement it as a command line option for your script if you wanted: the (non-core) argparse module is great for grabbing lists from command line options which you could then use to extend sys.path
mavnn
The command line option is to modify PYTHONPATH. It may or may not be abuse, depending on why you are doing it, so to speak. It's not abuse to change it if you want to avoid installing your libraries in the global python.
Lennart Regebro
+5  A: 

Take a look at tools like

  1. virtualenv, to set up a virtual python, in which you can install your modules without getting them globally. http://pypi.python.org/pypi/virtualenv

  2. Setuptools, which allows you to specify (and automatically install) dependencies for your modules. http://pypi.python.org/pypi/setuptools (If you have problems with setuptools, take a look at Distribute, a maintained fork. http://pypi.python.org/pypi/distribute )

  3. Buildout, which allows you deploy a complete application environment, including third-party software such as MySQL or anything else. http://pypi.python.org/pypi/zc.buildout/

Lennart Regebro
+1 for virtualenv + easy_install/setuptools
codeape
A: 

I just realised that I have actually solved this problem before. Here is the approach I used - much more complex than mavnn, but I was also solving the problem of running a Python2.x program from a Python 3.0

import os
import subprocess
env=os.environ.copy()
env['PYTHONPATH']=my_libraries
kwargs={"stdin":subprocess.PIPE, "env":env}
subprocess.Popen(["python","-u",program_path],**kwargs)
Casebash