How to interpret "from . import function_name"? [duplicate] - python

This question already has answers here:
What does a . in an import statement in Python mean?
(3 answers)
Closed 2 years ago.
Inside numpy.fft._pocketfft.py you can find import commands like:
from . import _pocketfft_internal as pfi
How would I interpret this line?

. means from current package. Hence, this line means:
from . import _pocketfft_internal as pfi
Import _pocketfft_internal from current package, you should be able to see a file named _pocketfft_internal.py in the current directory. You can study more about this from here

Related

Importing python files in another python file [duplicate]

This question already has answers here:
How do I import other Python files?
(23 answers)
Closed 2 years ago.
I have a folder of this file structure:
Desktop
tech_comp
googledash.csv
viz.py
sql_load.py
company_AL
__init__.py
functionloads.py
decisionplans.py
niches.py
actions.py
I am working with VScode in the following path as described above: C:\Users\username\Desktop\tech_comp\company_AL
I have written a bunch of list in the decisionplans.py now I am trying to load it in the actions.py I am working with. Here is what I did.
from company_AL import decisionplans
It does not show errors in the compiler but when I run I get the following.
ModuleNotFoundError: No module named 'company_AL'
I do not intend to publish it as online as this is a private project, please how do I handle this?
Thanks in advance
You have many solutions to solve your problem :
1. Add compagn_AL folder to your PYTHON_PATH
It depends of your OS but there is tutorials that explians better than me
2. Change the PATH for your script
import sys
sys.path.append('../') # or "C:\Users\username\Desktop\tech_comp\"
and then
from compagny_AL import decisionplans
3. Import it directly (not recommended)
You can just
import decisinplans
I think you just need to do import decisionplans or from .company_AL import decisionplans because you are already in the company_AL.

Get system file type names python [duplicate]

This question already has answers here:
How can I check the extension of a file?
(14 answers)
How to check type of files without extensions? [duplicate]
(10 answers)
Closed 4 years ago.
Is there any way to retrieve a file type name in Python using the OS module?
An example made up command:
>>> os.file_type('txt')
Would return:
'Text Document'
Any help would be appreciated :)
Oscar.
For getting file type you need to check the extension of the file
I think this can help.
import os
if os.path.splitext(file)[1] == ".txt":
pritn 'Text Document'
For os related task you can look over this doc.
https://github.com/Projesh07/Python-basic/blob/master/python_os_module/python_os_module.py

Why when importing base64 in python my prints are done twice (x2)? [duplicate]

This question already has answers here:
Why is this Python code running twice? [duplicate]
(2 answers)
Closed 6 years ago.
import base64
image_base64 = 'Hello World\n'
print image_base64
print 'Hello?\n'
Result:
Hello World
Hello?
Hello World
Hello?
Your file must be named base64.py so when you import base64 at the top of the file, it is importing itself causing the print statements to execute twice (once on the import and once afterwards).
You should rename your script to something whose name does not conflict with the name of a standard module.

Python import statement [duplicate]

This question already has answers here:
What does a . in an import statement in Python mean?
(3 answers)
Closed 7 years ago.
Is there a difference between these two import statements in python
1) from .models import Q
2) from models import Q
I am a little bit confused with the dot, because models.py and file.py(where import is executed) are in the same directory.
Any help will be appreciated.
The dot tells python that the module is in the current directory. Without the dot, python will look up the module following standard procedures (from the system path, python path, current directory etc.). You can read more about it here: https://docs.python.org/2/tutorial/modules.html, paying attention to the relative path part

Python: Get script location [duplicate]

This question already has answers here:
How do you properly determine the current script directory?
(16 answers)
Closed 8 years ago.
I've got a question.
How can a program get its own location?
For example, I've got a script ("script.py") on a path ("C:\Programs\script.py").
I want a function, which gives me the path. Like the following:
scriptdic() ==> "C:/programs/"
Thanks for coming answers.
this should do the trick. . .
import os.path
import sys
os.path.dirname(sys.argv[0])

Categories