4

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:

Community
  • 1
  • 1
Casebash
  • 114,675
  • 90
  • 247
  • 350

5 Answers5

10

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
  • 9,101
  • 4
  • 34
  • 52
  • 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 Sep 25 '09 at 09:11
  • To the best of my knowledge this _is_ the "proper" way. I'm curious what's wrong with it? – mavnn Sep 25 '09 at 09:23
  • Nothing is wrong with it. I just would have expected a command line option. – Casebash Sep 25 '09 at 09:26
  • 1
    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 Sep 25 '09 at 09:34
  • 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 Sep 25 '09 at 11:28
5

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
  • 167,292
  • 41
  • 224
  • 251
1

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
  • 127,331
  • 53
  • 180
  • 211
  • It's irritating that symlinks aren't as flexible in windows as well. – mavnn Sep 25 '09 at 08:42
  • 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 Sep 25 '09 at 08:54
  • 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 Sep 25 '09 at 09:00
  • That would unfortunately be inconsistent with how Eclipse handles importing other projects – Casebash Sep 25 '09 at 09:28
0

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

Community
  • 1
  • 1
Joril
  • 19,961
  • 13
  • 71
  • 88
0

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
  • 114,675
  • 90
  • 247
  • 350