Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Referencing another project

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:

  • Referencing another project: This question is basically asking how to import
like image 531
Casebash Avatar asked Dec 06 '22 04:12

Casebash


2 Answers

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.

like image 193
mavnn Avatar answered Dec 07 '22 18:12

mavnn


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/

like image 45
Lennart Regebro Avatar answered Dec 07 '22 18:12

Lennart Regebro