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.
Related
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:
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
This question already has answers here:
How to temporarily modify sys.path in Python?
(4 answers)
Closed 8 years ago.
import sys
print(sys.path)
'C:\\python32\\Lib\\site-packages\\django'
'C:\\Python32'
'C:\\Python32\\lib\\site-packages'
...
for some reason, my pythonpath got messy. I'd like to organize it. Is it correct that I don't need the first and the last one above? And how can I change it permanently? (Not like sys.path.remove or sys.path.append)
I'm using Python3.2. in windows8.
Path 'C:\Python32\lib\site-packages' is added to sys.path by the built-in site module.
If you want to, you can start python with the -S flag to tell the site module "Don't add site-packages".
python -S
Next, 'C:\python32\Lib\site-packages\django'.
Here's a wild guess: you installed django with pip/easy_install/msi-installer and there is a file
C:\python32\Lib\site-packages\django.pth (or something like this ending with .pth)
Quoting the docs:
A path configuration file is a file whose name has the form name.pth
and exists in one of the four directories mentioned above; its
contents are additional items (one per line) to be added to sys.path.
You can remove django.pth file (not recommended, see below) to remove '..../django' from sys.path
So, short answer: don't mess with sys.path, what's in sys.path is probably for a good reason.
If you don't need django, then uninstall django using whatever tool you used to install it. Same for every package you don't need.
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])
I have a Python script in a file stored in a Folder MyFolder. Tree structure for the file system is the following
MyFolder
- Image
- scipt_image.py
- script.py
- script_test.py
I want to use scripts in Image, scipt_image.py, in script script.py. To include this script_image.py, I use this :
import os, sys
DATA_DIR = os.path.abspath(os.path.join(os.getcwd(), 'Image'))
sys.path.append(DATA_DIR)
Then, i import script_image.py, with
import scipt_image
From errors at compilation, this seems not be working. However, problem may come from something else. Does this sounds correct ?
thanks
Is there a specific reason you don't simply use the Image folder as a python package and import the file directly without messing with sys.path?
I would simply turn Image into a python package (create an empty file named __init__.py in Image) and then import script_image like this:
from Image import script_image