Spyder reorders my import statements - breaks function - python-3.x

I am using Spyder as Editor/IDE.
I play with the options to include files/function/modules form other olders just to get a better understanding of how it works.
But Spyder rearranges my import statements in alphabetical order and thus breaks my functionality:
This is what I need:
import sys
sys.path.insert(1,'d:/pathtohelloworld/')
import helloworld
But when I do "Save File" in Spyder, it rearranges to
import helloworld
import sys
sys.path.insert(1,'d:/pathtohelloworld/')
and of course it will fail, since it cannot import "helloworld" before the path is defined via sys.path.insert.
This alphabetical order might be a good python style, but how can I fix this issue ?

Related

inspect.py file in folder makes importing pandas not work anymore

I am sorry if this is a silly question but I came across a wierd behaviour. I have a folder with some files, one of them named inspect.py
However, if I change the name inspect.py to somethingelse.py, importing pandas starts working.
I would really like to understand why this is. I assume it has something to do with the module called inspect which (I THINK??) comes by default installed.
Can anyone help me understand this, please?
Looking a np.ma.core.py I see
import builtins
import inspect
import operator
import warnings
import textwrap
import re
These are all base Python modules. Your local inspect.py gets imported instead, which messes with the importing the rest of np.ma.core, and numpy in turn. And pandas depends on numpy.

How Can I Import A Python 3 Module With A Period In The Filename?

What is the proper way to import a script that contains a period, such as program_1.4.py, ideally using importlib?
(Now that the imp module is deprecated, this this answer no longer applies: How to reference python package when filename contains a period .)
After looking through the CPython quite a lot and coming back to some other solutions (especially Import arbitrary python source file. (Python 3.3+)), I realized that I needed to pass the full path to my module. Here is the cross-platform, call-location-independent solution:
"""
import os, sys # For running from Notepad++ shortcut, etc
import importlib.machinery
program_1_4 = importlib.machinery.SourceFileLoader('program_1.4', os.path.join(sys.path[0], 'program_1.4.py')).load_module()
print(program_1_4)
program_1_4.main()
"""

Doubts on openpyxl, python3

He guys, I am just a beginner in Python3. I have a question :
import openpyxl
from openpyxl.style import *
As you can see that I am importing the openpyxl module, but why I need to import the second one in order to style fonts and cells an on.
openpyxl is a package. It contains modules, such as style. You should import the package anyway (all package or individual items). You can either:
import openpyxl
from openpyxl.style import *
then use style items like item1, item2
or
from openpyxl import style
then use style items like style.item1, style.item2
You don't have to - you can just as easily do:
import openpyxl
openpyxl.styles.fonts()
Or:
from openpyxl import style
style.fonts()
It comes down to personal preference. Using * imports is generally frowned upon because there's a risk of polluting the namespace, but if you know this isn't going to happen, and you want to keep your lines of code shorter, it's acceptable.
You are importing openpyxl, which includes everything in openpyxl including openpyxl.style and everything inside that. But say you would like to use X function of openpyxl.style, then you would have to write:
openpyxl.style.X()
If you write the second line you can simpy write:
X()
Basically the second line imports all the contents of the namespace openpyxl.style into your current namespace, removing the hassle of having to write openpyxl.style. everytime. Although it is generally a good practice to not merge namespaces like this, and not use
from _________ import *
Rather you can write
import openpyxl.style as op
and then use the X function as :
op.X()
You can also omit the line
import openpyxl
if you are not using anything else from openpyxl other than that included in openpyxl.style

Why don't I need to import sys?

I have 2 python scripts, both utilizing sys.stdout, sys.exit(), etc. In one script, PyCharm highlights "import sys" as gray, (meaning it is never used), and if I remove the import statement, the program works just fine, including sys.stdout and sys.exit().
However, the second module does not highlight "import sys" as gray, and if I try to run it without that statement, I get an error on the first occurrence of sys.stdout:
NameError: name 'sys' is not defined
I have looked up the official documentation for sys, which says
"This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available."
Yet, most guides or instructions on how to use sys tell you to import.
So, do I have to import it or not? Why does one program need to, but not the other?
Possibly important differences between the two programs:
One program has a main function, and imports the other program. This is the one that does not need to import sys. Perhaps it inherently imports sys when it imports the other one as a module?
It seems that the first script does not just import the second script; it imports * from it, like this:
in module_1:
from module_2 import *
And in module_2:
import sys
This causes module_1 to import sys, indirectly. If I change
from module_2 import *
to
import module_2
then it no longer works.

python 3 tkinter Pycharm - error on messagebox

Im writing a GUI and I say:
from tkinter import *
Further in the program theres a function wich is:
def nameFunc():
messagebox.showinfo(........)
The problem is that by running the code in the latest Pycharm, it tells me that messagebox is not defined even if I already imported everything from tkinter, it only works if I explicitly say:
from tkinter import messagebox
This only occurs when I run the code on Pycharm, in the standard python IDLE its fine.
Why?
PyCharm is behaving exactly as it should, if you take a look at the documentation on packages:
what happens when the user writes from sound.effects import *? Ideally, one would hope that this somehow goes out to the filesystem, finds which submodules are present in the package, and imports them all. This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.
The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered.
tkinter does not define a __all__ to automatically import submodules and you should be glad it doesn't import them all automatically:
import tkinter.__main__
print("this will only print after you close the test window")
the program only continues to run after a window pops up with the current tcl/Tk version and some other content is closed, to import submodules of the package you must explicitly import them with:
from tkinter import messagebox
however as I describe in my other answer here, because of how IDLE is built it has already loaded some of the submodules when your code is being executed in the idle Shell.

Resources