Imports

G

gtb

I am working with a tool called maxQ that generates jython scripts.
The tool runs in

C:\maxq\bin.


Rather than clutter up bin I want to put the scripts and other .py
scripts in

c:\maxq\bin\testScripts.


When doing so the scripts need to import from

c:\maxq\jython, but can't find the modules.


I tried adding c:\maxq\jython to the environment variable pythonpath
but to no avail. Perhaps I missed it in Rossum's tutorial but is there
a std prgrammatic way to specify a module directory?

Could you, would you, recommend a text or another webpage for learning
more (much more) about python?

Thanx,

gtb
 
K

kyosohma

I am working with a tool called maxQ that generates jython scripts.
The tool runs in

C:\maxq\bin.

Rather than clutter up bin I want to put the scripts and other .py
scripts in

c:\maxq\bin\testScripts.

When doing so the scripts need to import from

c:\maxq\jython, but can't find the modules.

I tried adding c:\maxq\jython to the environment variable pythonpath
but to no avail. Perhaps I missed it in Rossum's tutorial but is there
a std prgrammatic way to specify a module directory?

Could you, would you, recommend a text or another webpage for learning
more (much more) about python?

Thanx,

gtb

I typically just import sys and then do a
sys.path.append(directoryPath). This basically makes whatever modules
in that path available at run time. If you need a beginners reference
book, I recommend "Beginning Python" by Hetland. "Python Programming
for the Absolute Beginner" by Dawson was a lot of fun, but he doesn't
get into the internals of the language like Hetland does. Once you're
a snake charmer too, you can "graduate" to the "Programming Python"
book by Lutz.

Mike
 
G

gtb

I typically just import sys and then do a
sys.path.append(directoryPath). This basically makes whatever modules
in that path available at run time. If you need a beginners reference
book, I recommend "Beginning Python" by Hetland. "Python Programming
for the Absolute Beginner" by Dawson was a lot of fun, but he doesn't
get into the internals of the language like Hetland does. Once you're
a snake charmer too, you can "graduate" to the "Programming Python"
book by Lutz.

Mike

Something else going on then, does the directory path need to be the
full path or will it search sub-directories?

Traceback (most recent call last):
File "C:\maxq-0.98\bin\testScripts\compactLogin.py", line 2, in
<module>
from CompactTest import CompactTest
File "c:\maxq-0.98\jython\CompactTest.py", line 2, in <module>
from java.lang import *
ImportError: No module named java.lang

Thanks for the book recommendations.

gtb
 
K

kyosohma

Something else going on then, does the directory path need to be the
full path or will it search sub-directories?

Traceback (most recent call last):
File "C:\maxq-0.98\bin\testScripts\compactLogin.py", line 2, in
<module>
from CompactTest import CompactTest
File "c:\maxq-0.98\jython\CompactTest.py", line 2, in <module>
from java.lang import *
ImportError: No module named java.lang

Thanks for the book recommendations.

gtb

Usually, the sys.path.append will allow Python to search the
subfolders too, but I have noticed that sometimes it doesn't if you
use UNC paths. I don't use jython, so maybe there's another way to fix
this that is more specific to that implementation of Python?

Mike
 
G

gtb

Usually, the sys.path.append will allow Python to search the
subfolders too, but I have noticed that sometimes it doesn't if you
use UNC paths. I don't use jython, so maybe there's another way to fix
this that is more specific to that implementation of Python?

Mike

Thanks for the sys.path.append info. Turns out maxq cranks thing up
with java ( I should have seen it). The sys.path.append does
everything it supposed to do.

gtb
 
G

Gabriel Genellina

Usually, the sys.path.append will allow Python to search the
subfolders too, but I have noticed that sometimes it doesn't if you
use UNC paths. I don't use jython, so maybe there's another way to fix
this that is more specific to that implementation of Python?

Python does not search subfolders during a normal import. Perhaps you were
thinking about subpackages?
 
7

7stud

I typically just import sys and then do a
sys.path.append(directoryPath). This basically makes whatever modules
in that path available at run time. If you need a beginners reference
book, I recommend "Beginning Python" by Hetland. "Python Programming
for the Absolute Beginner" by Dawson was a lot of fun, but he doesn't
get into the internals of the language like Hetland does. Once you're
a snake charmer too, you can "graduate" to the "Programming Python"
book by Lutz.

Mike

I can't access any modules using the sys.path.append() method. Here
is my directory structure:

/
----Users
---------Me
------------2testing
------------------dir1
---------------------test1.py
---------------------programs_python
-----------------------------testA.py

testA.py:
------------------------
def show(x):
print x

if __name__ == "__main__":
show("hello")
------------------------

test1.py:
------------------------
import sys

sys.path.append("/Users/Me/2testing/dir1/programs_python")
testA.show("hello")
------------------------

command:
~/2testing/dir1$ python test1.py

output:
Traceback (most recent call last):
File "test1.py", line 4, in ?
testA.show("hello")
NameError: name 'testA' is not defined

Any idea how to do that?
 
7

7stud

I can't access any modules using the sys.path.append() method. Here
is my directory structure:

/
----Users
---------Me
------------2testing
------------------dir1
---------------------test1.py
---------------------programs_python
-----------------------------testA.py

testA.py:
------------------------
def show(x):
print x

if __name__ == "__main__":
show("hello")
------------------------

test1.py:
------------------------
import sys

sys.path.append("/Users/Me/2testing/dir1/programs_python")
testA.show("hello")
------------------------

command:
~/2testing/dir1$ python test1.py

output:
Traceback (most recent call last):
File "test1.py", line 4, in ?
testA.show("hello")
NameError: name 'testA' is not defined

Any idea how to do that?

Hmmm...I got it to work like this:

test1.py:
---------
import sys
sys.path.append("/Users/Me/2testing/dir1/programs_python")

import testA
testA.show("hello")
 
S

Steve Holden

7stud said:
Hmmm...I got it to work like this:

test1.py:
---------
import sys
sys.path.append("/Users/Me/2testing/dir1/programs_python")

import testA
testA.show("hello")
That's how it's s'posed to work. You still have to explicitly import the
modules you want - the sys.path just tells Python where to look for them.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top