Re: user defined modules
Sharan Basappa wrote: > Is there a specific location where user defined modules > need to be kept? My advice is that any location is a good location so long as the location you chose is _not_ a part of the PythonXY directory tree. For example, on a windoze machine (and in Python2.x at least), the PythonXY directory is placed (by default) at the root of the default drive (usually C drive). Your scripts (on a windows machine) should be (idealy) somewhere in the os.path.expanduser('~/Documents') tree. Personally i use a .pth file to point Python in the direction of my library modules. Unlike the old time masochist, i just hate having to repeat myself. Thus, like most modern folk, i have learned that copy+paste and "set- it-and-forget-it" text file are a computer users best friend and loyal companions. ## MAP ## C:/ PythonXY/ config.pth The contents of my .pth file is simply an unquoted string representing the path to my library folders. (something like this) ## PTH FILE CONTENT ## C:/.../Documents/path/to/my/lib/folder That's it! -- https://mail.python.org/mailman/listinfo/python-list
Re: user defined modules
On 5 Jun 2018 09:32, Steven D'Aprano wrote: On Mon, 04 Jun 2018 20:13:32 -0700, Sharan Basappa wrote: > Is there a specific location where user defined modules need to be kept? > If not, do we need to specify search location so that Python interpreter > can find it? Python modules used as scripts can be run from anywhere, by pointing the interpreter at the script: python /path/to/my/script.py But Python modules uses as libraries, to be imported by other modules, have to be on the Python search path. You can add extra paths to the Python search path from the shell by setting the environment variable PYTHONPATH to a colon-separated list of paths. On Linux, I do this in my .bashrc config file: export PYTHONPATH="paths:to:add" In the Python interpreter, you can query and modify the search path by importing sys and looking at sys.path. (But you should not do so unless you really know what you are doing.) The default search path is set by the site module: https://docs.python.org/3/library/site.html but again, you should not mess with this unless you know what you are doing. There are some per-user directories which are automatically added to the search path. I can't find the existing documentation for them, but a good place to start is the PEP that introduced the feature: https://www.python.org/dev/peps/pep-0370/ Apart from setting the PYTHONPATH environment variable, the best way to add extra paths to is to install a .pth file. If you run both Python 2 and 3, than .pth might be a better choice. With Pythonpath, you run the risk of e.g. importing python3-incompatible code. -- https://mail.python.org/mailman/listinfo/python-list
Re: user defined modules
On Mon, 04 Jun 2018 20:13:32 -0700, Sharan Basappa wrote: > Is there a specific location where user defined modules need to be kept? > If not, do we need to specify search location so that Python interpreter > can find it? Python modules used as scripts can be run from anywhere, by pointing the interpreter at the script: python /path/to/my/script.py But Python modules uses as libraries, to be imported by other modules, have to be on the Python search path. You can add extra paths to the Python search path from the shell by setting the environment variable PYTHONPATH to a colon-separated list of paths. On Linux, I do this in my .bashrc config file: export PYTHONPATH="paths:to:add" In the Python interpreter, you can query and modify the search path by importing sys and looking at sys.path. (But you should not do so unless you really know what you are doing.) The default search path is set by the site module: https://docs.python.org/3/library/site.html but again, you should not mess with this unless you know what you are doing. There are some per-user directories which are automatically added to the search path. I can't find the existing documentation for them, but a good place to start is the PEP that introduced the feature: https://www.python.org/dev/peps/pep-0370/ Apart from setting the PYTHONPATH environment variable, the best way to add extra paths to is to install a .pth file. See here: https://pymotw.com/2/site/#path-configuration-files > Also, when does Python interpreter compile the module code? When it is > imported? Yes. Executing a module as a script does not compile it. But when it is imported, it will be compiled to byte-code the first time, and then the byte-code will be used. You can force the compilation using the compileall: https://docs.python.org/3/library/compileall.html Or just import the module from the interactive interpreter. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
user defined modules
Is there a specific location where user defined modules need to be kept? If not, do we need to specify search location so that Python interpreter can find it? Also, when does Python interpreter compile the module code? When it is imported? -- https://mail.python.org/mailman/listinfo/python-list
Re: jython: user-defined modules
Gregor Stich wrote: > Dear all, > > I hope my question is here in the right place... > What I want to achieve is a communication between Java and Python. We > have a pretty strong framework of existing python scripts and modules. > Now I want to use jython in order to faciliate the communication > between another Java framework. > > But I`m currently stuck with jython: Trying to import the user-defined > python modules, these cannot be found. I read a lot about the jython > registry and I setup a $HOME/.jython file where I created a > python.path=... resource, however, when I start the jython command > line and type > print python.path > then this property is not found. Passing it with the -D option does > not work as well, or maybe I`m too confused to make it work anyway? First of all, you should subscribe to the jython mailing list. Second, you access the python.path inside jython via import sys print sys.path HTH, Diez -- http://mail.python.org/mailman/listinfo/python-list
jython: user-defined modules
Dear all, I hope my question is here in the right place... What I want to achieve is a communication between Java and Python. We have a pretty strong framework of existing python scripts and modules. Now I want to use jython in order to faciliate the communication between another Java framework. But I`m currently stuck with jython: Trying to import the user-defined python modules, these cannot be found. I read a lot about the jython registry and I setup a $HOME/.jython file where I created a python.path=... resource, however, when I start the jython command line and type print python.path then this property is not found. Passing it with the -D option does not work as well, or maybe I`m too confused to make it work anyway? Any help would be great! Best regards Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: import function from user defined modules
groves wrote: > Can anybody give me an example of how to import a function of module X > in module y. And please if yu can use classes(Object oriented approach) > would be great. > > The problem is that I have created a text on canvas, and now I want > that whenever a user right clicks on it, the option COMMAND should > invoke a function defined in some other module say Y. import Y widget = Canvas(..., command=Y.function) for more on this, see the tutorial: http://docs.python.org/tut/node8.html -- http://mail.python.org/mailman/listinfo/python-list
Re: import function from user defined modules
groves wrote: > Can anybody give me an example of how to import a function of module X > in module y. And please if yu can use classes(Object oriented approach) > would be great. > > The problem is that I have created a text on canvas, and now I want > that whenever a user right clicks on it, the option COMMAND should > invoke a function defined in some other module say Y. > > thanks a lot to all who will look into problem, any help would be > appreciated. from X import func Then you can call func() in your module. Peace, ~Simon -- http://mail.python.org/mailman/listinfo/python-list
import function from user defined modules
Can anybody give me an example of how to import a function of module X in module y. And please if yu can use classes(Object oriented approach) would be great. The problem is that I have created a text on canvas, and now I want that whenever a user right clicks on it, the option COMMAND should invoke a function defined in some other module say Y. thanks a lot to all who will look into problem, any help would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
import function from user defined modules
Can anybody give me an example of how to import a function of module X in module y. And please if yu can use classes(Object oriented approach) would be great. The problem is that I have created a text on canvas, and now I want that whenever a user right clicks on it, the option COMMAND should invoke a function defined in some other module say Y. thanks a lot to all who will look into problem, any help would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing User-defined Modules
Thomas Guettler, your solution was very impractical besides being ignorant of my problem. Thanks for trying nonetheless. Andrew Clover, that is exactly what I was looking for. A few examples would have been nice, but I think I can manage from here. Thanks you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing User-defined Modules
Walter Brunswick <[EMAIL PROTECTED]> wrote: > I need to import modules with user-defined file extensions > that differ from '.py', and also (if possible) redirect the > bytecode output of the file to a file of a user-defined > extension. You shouldn't really need a PEP for that; you can take control of the compile and import processes manually. See the py_compile and imp modules. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing User-defined Modules
Am Sun, 24 Jul 2005 13:12:04 -0400 schrieb Walter Brunswick: > I need to import modules with user-defined file extensions that differ from > '.py', and also (if possible) redirect the bytecode > output of the file to a file of a user-defined extension. > I've already read PEP 302 (http://www.python.org/peps/pep-0302.html), but I > didn't fully understand it. Would someone [who > understands it] please be able to give me a synopsis of it, along with a few > explanatory examples, or some other alternatives, if > any? You could do it like this: Copy the file to directory which is in sys.path. Give the file the extension ".py". Import it with __import__ (http://docs.python.org/lib/built-in-funcs.html#l2h-6) Copy the bytecode somewhere. HTH, Thomas -- Thomas Güttler, http://www.thomas-guettler.de/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing User-defined Modules
Walter Brunswick wrote: I need to import modules with user-defined file extensions that differ from '.py', and also (if possible) redirect the bytecode output of the file to a file of a user-defined extension. I've already read PEP 302 (http://www.python.org/peps/pep-0302.html), but I didn't fully understand it. Would someone [who understands it] please be able to give me a synopsis of it, along with a few explanatory examples, or some other alternatives, if any? Well, under the PEP302 regime, as well as all the usual suspects you can add to sys.path you can also add "importer" objects. These objects will have specific methods (find_module and load_module) called at specific times during the import process. Because there can be a significant amount of time spent initializing an importer object the system now uses a cache of importers, which it's sensible to clear when adding a new importer. I attach code below that implements an importer that loads modules from a database, plus the program that actually creates the database containing the modules. This was only a test of my understanding, but it may help you to move further towards PEP302. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ dbload.py Description: application/python dbimp.py Description: application/python -- http://mail.python.org/mailman/listinfo/python-list
Importing User-defined Modules
I need to import modules with user-defined file extensions that differ from '.py', and also (if possible) redirect the bytecode output of the file to a file of a user-defined extension. I've already read PEP 302 (http://www.python.org/peps/pep-0302.html), but I didn't fully understand it. Would someone [who understands it] please be able to give me a synopsis of it, along with a few explanatory examples, or some other alternatives, if any? -- http://mail.python.org/mailman/listinfo/python-list