0

I'm new to python coming from java. I created a folder called: 'Project'. In 'Project' I created many packages (with __init__.py files) like 'test1' and 'tests2'. 'test1' contains a python script file .py that uses scripts from 'test2' (import a module from test2). I want to run a script x.py in 'test1' from command line. How can I do that?

Edit: if you have better recommendations on how I can better organize my files I would be thankful. (notice my java mentality)

Edit: I need to run the script from a bash script, so I need to provide full paths.

filmor
  • 30,840
  • 6
  • 50
  • 48
Jack Twain
  • 6,273
  • 15
  • 67
  • 107
  • If I am understanding your question correctly just give your script execution permissions (`./x.py` in *NIX systems) or type `python x.py`. – angryfruitsalad Mar 25 '14 at 12:57

2 Answers2

1

There are probably several ways to achieve what you want.

One thing that I do when I need to make sure the module paths are correct in an executable scripts is to get the parent directory and insert in the module search path (sys.path):

import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

import test1 # next imports go here...
from test2 import something
# any import what works from the parent dir will work here

This way you are safe to run your scripts without worrying how the script is called.

Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
1

Python code is organized into modules and packages. A module is just a .py file that can contain class definitions, function definitions and variables. A package is a directory with a __init__.py file.

A standard Python project might look something like this:

thingsproject/
  README
  setup.py
  doc/
     ...
  things/
    __init__.py
    animals.py
    vegetables.py
    minerals.py
  test/
    test_animals.py
    test_vegetables.py
    test_minerals.py

The setup.py file describes the metadata about your project. See Writing the Setup Script and particularly the section on installing scripts.

Entry points exist to help distribute command line tools in Python. An entry point is defined in setup.py like this:

setup(
    name='thingsproject',
    ....
    entry_points = {
        'console_scripts': ['dog = things.animals:dog_main_function']
    },
    ...
)

The effect is that when the package is installed using python setup.py install a script is automatically created in some reasonable place according to your OS, such as /usr/local/bin. The script then calls the dog_main_function in the animals module of the things package.

Yet another Python convention to consider is have a __main__.py file. This signifies the "main" script within a directory or zip file full of python code. This is a good place to define a command line interface to your code using the argparse parser for command line arguments.

Good and up-to-date information on the somewhat muddled world of Python packaging can be found in the Python Packaging User Guide.

Community
  • 1
  • 1
cbare
  • 12,060
  • 8
  • 56
  • 63