Re: Trouble with making modules 'global'
[email protected] wrote: > Hi, > > I am struggling with making modules global for all definitions in my code. Don't. Global names in Python are global to a single module, not your entire application. > I faced the problem with the pyomo modules but can generate the error with > pandas too. > > The model structure looks as follow: > > I have 3 '.py' files each of them containing definitions. The first > controls inputs and outputs and starts an optimization.py file ( > optimization.py). Within optimization.py I call a third .py file > (model.py) that includes a series of definitions, representing > mathematical equations of a optimization model. > > > > (1) Main.py > import pandas as pd If you want to use Optimization in Main.py you have to import the module just like you did with pandas. > > def main_part() > ... Result = optimization_def(x,y,z) > ... > > (2) Optimization.py If you want to use pandas in Optimization.py you have to import it. Note that only the first time a module is imported in an application the import may take a noticeable time. Subsequent imports result in a lookup in the sys.modules cache. > def optimization(x_l,y_l,z_l) > ... O = obejctive(x_l,y_l) > ... > > (3) Model.py > def objctive(x_a,x_b) > ... > def constraint(x_a,z_a) > > > > I do not understand why pd is not known in def optimization() as well as > in objctive(). I tried to make it global by adding global pd in Main.py > > > I would appreciate any support. Thanks in advance > Frank -- https://mail.python.org/mailman/listinfo/python-list
Re: Trouble with making modules 'global'
> On 4 Jun 2020, at 04:48, [email protected] wrote: > > Hi, > > I am struggling with making modules global for all definitions in my code. Each python module has to import all the modules it needs. Importing on one module does not make the imported names usable from another module. What python does do is only run the code of an imported module once. The seconds module to do an import will find that its already imported and just setup the names to be usable. > I faced the problem with the pyomo modules but can generate the error with > pandas too. > > The model structure looks as follow: > > I have 3 '.py' files each of them containing definitions. The first controls > inputs and outputs and starts an optimization.py file ( optimization.py). > Within optimization.py I call a third .py file (model.py) that includes a > series of definitions, representing mathematical equations of a optimization > model. > > > > (1) Main.py > import pandas as pd > > def main_part() > ... Result = optimization_def(x,y,z) > ... > > (2) Optimization.py You also need to import pandas in each module that uses it. > def optimization(x_l,y_l,z_l) > ... O = obejctive(x_l,y_l) > ... > > (3) Model.py And here. > def objctive(x_a,x_b) > ... > def constraint(x_a,z_a) > > > > I do not understand why pd is not known in def optimization() as well as in > objctive(). > I tried to make it global by adding global pd in Main.py > > > I would appreciate any support. Thanks in advance > Frank Barry > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Unsubscribe to python list
On Thu, Jun 4, 2020 at 12:14 AM DL Neil via Python-list wrote: > > On 4/06/20 4:59 PM, Meet Agrawal wrote: > > I want to unsubscribe from python list and would like to stop recieving > > mails from the same. > > > At the bottom of your request (as reflected), this, and all over > messages to the list is an administration link. At the foot of that > web-page you will find a space to unsubscribe your email address > (admittedly, I've not tried it). > -- Not that I have any wish to but wanting to 'know' I did check. The very last lines are where you enter your email address with the idea (didn't try that but ISTR) that you would get an automated response which would detail your options for account changes, which would include removing one's subscription. HTH -- https://mail.python.org/mailman/listinfo/python-list
Fwd: How can I build embeddable zip python on Linux
Dear Python team, I have tried to build the GDB with Python support for windows. Because I didn’t want to have any Python dependence on building system, I decided to embed python in GDB using Windows x86_64 embeddable zip file and with it I had success. When I tried to build GDB with Python support on Linux, I realized I couldn’t find embeddable python for Linux. I have tried to find some article how can I build embeddable Python from source in Linux and only what I have found is this: https://stackoverflow.com/questions/42280553/how-to-build-embeddable-python but again it is only for Windows. Please, can you give me some instructions how can I build embeddable Python from source in Linux? Is it possible in the Linux environment? Please let me know if I can assist any further. With best regards, M.Eng. Filip Bascarevic -- https://mail.python.org/mailman/listinfo/python-list
Re: Trouble with making modules 'global'
On 6/3/2020 11:48 PM, [email protected] wrote: I am struggling with making modules global for all definitions in my code. You cannot. You must import every module either directly or indirectly into every module that uses it. I faced the problem with the pyomo modules but can generate the error with pandas too. The model structure looks as follow: I have 3 '.py' files each of them containing definitions. The first controls inputs and outputs and starts an optimization.py file ( optimization.py). Within optimization.py I call a third .py file (model.py) that includes a series of definitions, representing mathematical equations of a optimization model. (1) Main.py The PEP8 standard is all lower case for modules, leaving TitleCase for classes, so, for instance, file/module 'editor' contains the definition of class 'Editor' as its main class. import pandas as pd Do you use 'pd' in Main? If not, delete. def main_part() ... Result = optimization_def(x,y,z) ... (2) Optimization.py To use pd here, import it here. def optimization(x_l,y_l,z_l) ... O = obejctive(x_l,y_l) ... (3) Model.py Ditto. def objctive(x_a,x_b) ... def constraint(x_a,z_a) I do not understand why pd is not known in def optimization() as well as in objctive(). Because of missing imports. I tried to make it global by adding global pd in Main.py Where it is likely irrelevant. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Division issue with 3.8.2 on AIX 7.1
On 6/3/2020 8:11 PM, Dennis Lee Bieber wrote: On Tue, 2 Jun 2020 12:26:16 -0500, Albert Chin declaimed the following: I've built Python 3.8.2 on AIX 5.2, 5.3, 6.1, and 7.1. I am seeing different results for the following Python program: $ python3 -c "eps = 2.0 ** -53.0; tiny = 2.0 ** -1022.0; \ print ((1.0 - eps) / tiny * 4.0)" I get the correct result, 1.7976931348623157e+308, on AIX 5.2, 5.3, and 6.1. But, on 7.1, I get "inf". Execute "print(1.0-eps, tiny)" and see if they differ. Anyone know where can I look in the Python source code to investigate this? Have you considered that it might be something in an underlying C library (especially for the double-precision exponentiation)? This is a more likely source of the change than our code. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Division issue with 3.8.2 on AIX 7.1
On 6/3/20 10:37 PM, Sherry L. West wrote: > I need off this list please. I don’t even have this. Unsubscribe here: https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Python with text editor
On 5/30/20 1:42 AM, Preetha M wrote: > Hello. Thank you for responding to my previous mail. Can someone tell me > how to connect python to sublime text 3. Whenever I select python and type > the code, it does not work when I press ctrl+B. Please tell. > Try Corey Schaefer's video on setup: (search on youtube for "Setting up a Python Development Environment in Sublime Text" if you don't want to follow an obfuscated link in email, which is probably a good idea!) https://www.youtube.com/watch?v=xFciV6Ew5r4 -- https://mail.python.org/mailman/listinfo/python-list
Friday Finking: Imports, Namespaces, Poisoning, Readability
How do you prefer to balance all of the above when import-ing? Python offers a number of options for importing modules, eg import module, ... from importable import object, ... most of which can be augmented with the "as preferred_name" syntax. (ignoring the much-reviled "*" (import everything, not private) variation) The basic application of the as-clause, is a means of avoiding conflict within the namespace. Thus, if the name "vector" is already used within the module, we can: from importable import vector as direction ... go( direction ) Another common use is abbreviation - despite modern editors offering look-ahead guesses and superceding the typing-saver it once was; eg: import numpy as np from importable import vector as v The "np" is not as good an example (for this discussion) because it is very commonly-used and therefore well-understood (at least amongst the Stats and DataSc communities). However, the "v" suffers the same fate as abbreviated object-names everywhere - after a while, one starts to forget exactly what the opaque name actually means. In between the intense COVID-19 busy-ness, I have been attempting a contEd course (and perversely choosing to use Python instead of their chosen language) and thus am 'discovering' (PSL's) pygame library. Accordingly: import pygame as pg So, after employing that, every time I encountered the abbreviation I had to stop and think: 'what is this "pg" thing?'. Agreed, that's part of the "learning process" - and in-time, even my sluggish grey-matter managed to reach the point of readability (but can I assume the same of my reader/colleagues/course-tutor?). However, as I started building helper functions and my own objects 'around' the pygame library, without much thought, I followed the pattern: import pygame as pg # this is from PSL import pygame_utilities as pgu# this is from my own work ... if pgu.has_alpha( ink_color_tuple ): ... It didn't take long before I was being stopped, to think: "where did "pgu" come-from?" (remember, being a good, little, boy*, all of my import statements are parked way-up at the top of the code-listing). The opposite approach - specifically avoiding abbreviations and the readability issues they may carry, and bravely facing the challenge of avoiding name-clashes, might involve something like: from pygame_plane_geometry import ( CartesianError, GeometricNtuple, CartesianCoordinate, Vector, Polygon, ... ) ... position = CartesianCoordinate( 1, 2 ) Now, we will enjoy much-improved readability - but will have to rely upon the IDE's 'pop-up', to see an object's init/signature (for example). Thus, we face an extra concern of 'where do I find this module?' to be able to clarify or examine any detail. The final 'crunch' then - to enjoy full readability AND avoid naming conflict AND offer full source-location information: import pygame_plane_geometry ... position = pygame_plane_geometry.CartesianCoordinate( 1, 2 ) Lovely stuff! Except that we're either 'banging on the door' of column-79, or our eyes are tiring from 'all that reading'. Maybe? Finking/discussion: - how do you like to balance these three (and any other criteria)? - is your preference (or selection) influenced by the facilities offered by your favorite editor/IDE? - does your decision differ according to whether the 'target module' is one of yours, from the PSL, from some third party, corporate, ...? - do you prefer PSL's importlib over Python's native import-s, for one of these (or any other) reason? - AOB? WebRefs: https://docs.python.org/3/tutorial/modules.html https://docs.python.org/3/reference/import.html * if you believe that, I have a bridge from Brooklyn available at an absolute bargain price... (https://nycwalks.com/blog/the-brooklyn-bridge-if-you-believe-that-i-have-a-bridge-in-brooklyn-to-sell-to-you/) -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Friday Finking: Imports, Namespaces, Poisoning, Readability
On 2020-06-05 12:15, DL Neil via Python-list wrote:
> Finking/discussion:
>
> - how do you like to balance these three (and any other criteria)?
For most of what I do, I only ever have one such module so I'm not
trying keep multiple short-names in my head concurrently. For me,
it's usually tkinter which I import as either "tk" or just "t". It's
usually cases where the whole content would dump a lot of junk in my
module's namespace (like tkinter or pygame), so I like to keep the
namespacing; but I also don't want to clutter my code with long
references to it.
> - is your preference (or selection) influenced by the facilities
> offered by your favorite editor/IDE?
Not really. I'm a vi/vim/ed guy. While vim has some nice "go to
the definition of the thing under the cursor" (gd/gD/tags/etc),
regardless of my $EDITOR, it's not overly taxing to see "tk.thing"
and if I had any question about it, jump to the top of the file where
all the import-statements are, and see that tkinter is being imported
as tk. It's there explicitly, unlike those abominable "from tkinter
import *" tutorials.
> - does your decision differ according to whether the 'target
> module' is one of yours, from the PSL, from some third party,
> corporate, ...?
I've not given it much thought, but I tend to use the above method
with PSL/3rd-party libraries where there's a lot of things in that
namespace; but for my own code, they're usually pretty svelte, so I
stick to "from mymodule import a, b, c" if there are lots or just an
"import mymodule" without aliasing it shorter. If the imported names
were to grow to an unweildy size, I'd go the same route as above
("import myutils as u").
My largely irrelevant $0.02 USD, adjusted for tax and inflation,
-tkc
--
https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: How can I build embeddable zip python on Linux
On 6/4/20 2:47 AM, Filip Bascarevic wrote: > When I tried to build GDB with Python support on Linux, I realized I > couldn’t find embeddable python for Linux. > > Please, can you give me some instructions how can I build embeddable Python > from source in Linux? Is it possible in the Linux environment? What does "embeddable python" mean? Do you mean the libpython shared library and C API? If so it's just a matter of installing the correct -devel or -dev package on your distro. On Debian this would likely be libpython#.#-dev where # is the version. For example, libpython3.8-dev. On Fedora that might be python-devel (or on CentOS, python36-devel). -- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: How can I build embeddable zip python on Linux
On 5-6-2020 06:12, Michael Torrie wrote: On 6/4/20 2:47 AM, Filip Bascarevic wrote: When I tried to build GDB with Python support on Linux, I realized I couldn’t find embeddable python for Linux. Please, can you give me some instructions how can I build embeddable Python from source in Linux? Is it possible in the Linux environment? What does "embeddable python" mean? Do you mean the libpython shared library and C API? If so it's just a matter of installing the correct -devel or -dev package on your distro. On Debian this would likely be libpython#.#-dev where # is the version. For example, libpython3.8-dev. On Fedora that might be python-devel (or on CentOS, python36-devel). probably the same as asked here (with no answer (yet)): https://stackoverflow.com/questions/49425397/building-an-embeddable-minimal-python-distribution-on-linux -- Luuk -- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: How can I build embeddable zip python on Linux
On 5-6-2020 08:49, Luuk wrote: On 5-6-2020 06:12, Michael Torrie wrote: On 6/4/20 2:47 AM, Filip Bascarevic wrote: When I tried to build GDB with Python support on Linux, I realized I couldn’t find embeddable python for Linux. Please, can you give me some instructions how can I build embeddable Python from source in Linux? Is it possible in the Linux environment? What does "embeddable python" mean? Do you mean the libpython shared library and C API? If so it's just a matter of installing the correct -devel or -dev package on your distro. On Debian this would likely be libpython#.#-dev where # is the version. For example, libpython3.8-dev. On Fedora that might be python-devel (or on CentOS, python36-devel). probably the same as asked here (with no answer (yet)): https://stackoverflow.com/questions/49425397/building-an-embeddable-minimal-python-distribution-on-linux https://docs.python.org/3.5/using/windows.html#embedded-distribution 3.8. Embedded Distribution New in version 3.5. The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for acting as part of another application, rather than being directly accessed by end-users. When extracted, the embedded distribution is (almost) fully isolated from the user’s system, including environment variables, system registry settings, and installed packages. The standard library is included as pre-compiled and optimized .pyc files in a ZIP, and python3.dll, python35.dll, python.exe and pythonw.exe are all provided. Tcl/tk (including all dependants, such as Idle), pip and the Python documentation are not included. Note The embedded distribution does not include the Microsoft C Runtime and it is the responsibility of the application installer to provide this. The runtime may have already been installed on a user’s system previously or automatically via Windows Update, and can be detected by finding ucrtbase.dll in the system directory. Third-party packages should be installed by the application installer alongside the embedded distribution. Using pip to manage dependencies as for a regular Python installation is not supported with this distribution, though with some care it may be possible to include and use pip for automatic updates. In general, third-party packages should be treated as part of the application (“vendoring”) so that the developer can ensure compatibility with newer versions before providing updates to users. The two recommended use cases for this distribution are described below -- Luuk -- https://mail.python.org/mailman/listinfo/python-list
