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
Related
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
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.
I tried the answers in this question to no avail so I thought it'd be worth a separate question. The directory layout is as follows:
aale/
2/
2.py
__init__.py
3/
3.py
__init__.py
__init__.py
The names are unfortunately unable to change (this is the layout given to us for a HW problem). I am trying to import 2 from 3 but it doesn't seem to be working. I tried using importlib as:
two = importlib.import_module('2.2') as well as
two = importlib.import_module('2')
which didn't work also (gave a ModuleNotFoundError: No module named '2' error). Any help / other methods would be appreciated. I am using Python 3.6.
Assuming your script is in the aale directory, you will need to do your import like:
two = importlib.import_module('aale.2.2')
You can use __import__, i.e.:
two = __import__("2.2") # or __import__("aale.2.2")
three = __import__("3.3") # or __import__("aale.3.3")
Equivalent to:
import 2.2 as two
import 3.3 as three
which isn't possible.
Notes:
According to PEP 8 styling guide:
Package and Module Names should have short, all-lowercase names. Underscores can be used in the module name
if it improves
readability.
I've tested the imports using the same folder structure as in your
question and no errors were shown.
References:
https://stackoverflow.com/a/16644280/797495
In python, how to import filename starts with a number
PEP 8 -- Style Guide for Python Code
You can use pathlib
It is basic:
from pathlib import Path
BASE_LOCATION = Path(__file__).parent
(You can add .parent to get to the folder above and so on.)
Edit
If this doesn't help you, you can try import weirdimport.
You can read more about this here.
Hope this helps!
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
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])