3

I am about to start programming python with jython on WinXP (Later Win7). After I tried out the out of the box interpreter in the comand line, I want to try out programming standalone modules.

My questions are:

Where do I have to put the .py files?

How to run them?

How to import scripts which are not in the same directory like the 'main' script? (Import scripts/classes/functions from an relative/absolute path) I plan to have something like a workspace folder (e.g. 'C:\pythonWorkspace') where I build my own python script library for importing them in several projects.

Edit: Added used OS: WinXP/Win7

poeschlorn
  • 12,230
  • 17
  • 54
  • 65
  • 1
    using linux?win? basically you could both add a location pythonpath or just put the models inside libs\site-packages where python searchs anyway. you can run a module (if it has the __name__=="main" part) from the command line with the python interpreter using like "python modulename" – alonisser Dec 02 '11 at 16:50
  • Does this mean that I have to put all my file into the python\libs folder? How can I reference to a .py-file stored in C:\myProject ? I am not quite sure how to interpret the '__name__' expression – poeschlorn Dec 05 '11 at 12:51

2 Answers2

2

the relative/absolute path should in PYTHONPATH,sample code:

import os,sys
#change to your path
sys.path.append(os.path.join(os.path.dirname(__file__),'lib'))
Michael
  • 325
  • 1
  • 7
  • 'lib' means the path and __file__ the filename within the path? – poeschlorn Dec 05 '11 at 06:54
  • 1
    @poeschlorn: No, `__file__` is the filename of the file that contains the code that is running _right then_. It means "_this file_" – Bryan Oakley Dec 05 '11 at 14:00
  • ok. So am I right, that this snippet will add the path of the script (via __file__) to the PATH environment variable of my operating system? – poeschlorn Dec 05 '11 at 15:04
  • assume that your script file and sub-folder 'lib' are in same folder, the snippet will add 'lib' folder files to PYTHONPATH. from python doc: sys.path is A list of strings that specifies the search path for modules. – Michael Dec 06 '11 at 08:36
  • ok, but isn't "Lib" already part of the path (subdirectory from python root path)? I tried to import any other directory like C:\folder to the path...it doesn't work :( – poeschlorn Dec 07 '11 at 07:24
  • Try the absolute path first like `sys.path.append("/home/user/program/lib")` for linux users. That worked for me, too. But your answer is of course better, Michael. Happy, that I found it! – Lisa B. Mar 25 '21 at 08:55
0

you can add a pth file in lib\site-packages containing something like (example.pth):

c:\myProject

and then you can just import the module by name

in a module you write - if you want it to run something from command line (or from the open menu of the idle and than run) - in the end of the module you write:

if __name__=="main":
    do something
    print something
    run somefunction()#from the module
alonisser
  • 11,542
  • 21
  • 85
  • 139