I've tried adding the following line to my handler script (main.py), but it doesn't seem to work:
sys.path.append('subdir')
subdir lives in the my root directory (i.e. the one containing app.yaml).
This doesn't seem to work, because when I try to import modules that live in subdir, my app explodes.
1) Ensure you have a blank __init__.py file in subdir.
2) Use a full path; something like this:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir'))
Edit: providing more info to answer questions asked in a comment.
As Nick Johnson demonstrates you can place those three lines of code in a file called fix_path.py. Then, in your main.py file, do this import fix_path before all other imports. Link to a tested application using this technique.
And, yes, the __init__.py file is required; per the documentation:
When importing the package, Python
searches through the directories on
sys.path looking for the package
subdirectory.
The __init__.py files are required to
make Python treat the directories as
containing packages; this is done to
prevent directories with a common
name, such as string, from
unintentionally hiding valid modules
that occur later on the module search
path. In the simplest case,
__init__.py can just be an empty file, but it can also execute initialization
code for the package or set the
__all__ variable, described later.
It worked for me inserting the new dirs as the first entries in sys.path.
path_changer.py:
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'libs'))
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'apps'))
app.py:
import path_changer
from google.appengine.ext.webapp.util import run_wsgi_app
from flask import Flask
import settings
app = Flask('myapp')
app.config.from_object('settings')
from website import views as website_views
run_wsgi_app(app)
Related
I have a python project a folder structure like this:
main_directory
main.py
drivers
__init__.py
xyz.py
utils
__init__.py
connect.py
I want to import connect.py into xyz.py and here's my code:
from utils import connect as dc
But I keep getting this error no matter what I do, please help:
ModuleNotFoundError: No module named 'utils'
Update: People are telling me to set the path or directory, I don't understand why I need to do this only for importing file. This is something that should work automatically.
In your utils folder __init__.py should be blank. If this doesn't work, try adding from __future__ import absolute_import in your xyz.py file.
Check your current directory. It must be main_directory.
import os
print("Current working directory is: ", os.getcwd()
if not, you can change using
os.chdir("path/to/main_directory")
also works with relative path
os.chdir('..')
I was also facing same problem when you use row python script so i use the following code at the beginning of the file
import os
import sys
current_dir = os.path.dirname(os.path.realpath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)
This way it will search the require file at parent directory.
Hope this will work for you also..
You could move your utils folder into drivers, making the path to the to-be imported file a subdirectory of your executing file, like:
main_directory/drivers/utils/connect.py
Alternatively, you could try
from ..utils import connect as dc
This will move up a directory before import.
Lasty, you could add the directory to Path in your script via
import sys
sys.path.insert(0,'/path/to/mod_directory')
For this method, see also this question
I am having a very annoying problem with module imports within a Flask app.
This is my file structure:
root > app > views > tests
In the views folder, there is a file called data.py. I wish to import this into a file called test_data.py in tests:
from app.views import data
This gives a ModuleNotFound error, saying that app is not a module (I have added an __init__.py file, although I believe this is no longer needed in Python 3).
However, in the root (foo) folder, I have another file that is using exactly the same absolute import successfully:
from app.views import data
Can anybody help me out, please, with how I am able to import successfully into files other than the root folder?
So, if your test_data.py is inside tests, and you wish to import data.py from views folder, you need to tell python to look into the folder specifically. You can do this in your test_data.py using this:
import sys
sys.path.insert(1, '/root/app/views')
import data
remember that the path is the absolute path. If you are on windows, this would start from C:/ or something and on linux from root. Do not use the relative path but only absolute.
Hope this helps!
I need to import .py file from another one in another directory (import app2 from app1)
So there is directory tree
app:
dir1:
app1.py
dir2:
app2.py
My problem is almost like Importing from another directory, but this solution doesnt work for me for some reason
Furthermore i've been trying to do like this (app1.py)
from ..dir2 import app2
The error is:
Attempted relative import beyond top-level package
How can i solve this?
Add your additional directories to the system path
import sys
sys.path.insert(0, "/path/to/app1/dir2")
sys.path.insert(0, "/path/to/app2/dir2")
My directory structure looks like this:
I have some utility functions in util/misc.py that I want to import in compose_dataset.py. However, I cannot get the import statement to work.
I'm working on Windows Python3.5.4, so from what I've read I don't need __init__.py files anymore. The project folder is a child of my PYTHONPATH that points solely to E:\Python. So far, I tried:
from misc import *
from util import *
from util.misc import *
from ..util.misc import *
and either received ImportError: No module named 'xyz' or ImportError: attempted relative import with no known parent package. I also tried adding init-files but I have no experience with those and simply added one to every directory, but (surprisingly) that didn't work either.
What am I missing here??
Try this:
import sys
sys.path.insert(0, '../')
from util.misc import *
You may also want to take a look at this post: How to access a module from outside your file folder in Python?
Set your PYTHONPATH to ., and add __init__.py files into each directory, where you want to import from, in your case add it into src, data, util directories. Assuming that you run your "entry point" script from the root directory of your project (same where your README.md file is), to import use this code:
# compose_dataset.py file
from src.util.misc import function_name
Sorry, this is definitely a duplicate, but I can't find the answer. I'm working in Python 3 and this is the structure of my app:
/home
common.py
australia/
new-south-wales/
fetch.py
I am in the home/ directory, running fetch.py. How can I import functions from common.py in that script?
I've set up fetch.py as follows:
from common import writeFile
But I get the following error:
File "australia/new-south-wales/fetch.py", line 8, in <module>
from common import writeFile
ModuleNotFoundError: No module named 'common'
If I just do python -c "from common import writeFile" I don't see an error.
Shouldn't the interpreter look in the current directory for modules?
before import your directories that need to be imported must have file __init__.py in that folder
#solution 1 (import in runtime)
To import a specific Python file at 'runtime' with a known name:
import os
import sys
script_dir = "/path/to/your/code/directory"
# Add the absolute directory path containing your
# module to the Python path
sys.path.append(os.path.abspath(script_dir))
import filename
#solution 2(add files to one of python libraries)
also as you have a common library for you can run
>>> import sys
>>> print sys.path
and see what directories you can put your code and use in every project.you can move your common package to one of this directories and treat it like a normal package.for example for common.py if you put it in one root directory of one of this directory you can import like import common
#solution 3(use relative import)
# from two parent above current directory import common
# every dot for one parent directory
from ... import common
and then go to parent directory and run
python -m home.australia.new-south-wales.fetch
From the description I'm assuming you're not running this as complete python package, just as separate files.
What you can do is use complete modules. This means adding empty __init__.py to directories with your code. You'll also have to change the name of new-south-wales to new_south_wales, since it needs to be a valid module name.
Assuming home is the name of your app, you should end up with:
home/
__init__.py
common.py
australia/
__init__.py
new_south_wales/
__init__.py
fetch.py
Next, you'll need a startup script for your app - this means either something simple like:
#!/usr/bin/env python
from australia.new_south_wales import fetch
fetch.your_main_function()
Or you can add a setup.py with a full package description. If you specify entry points and the script will be automatically created.
Now that you're starting your code in context of a package, your fetch.py can do:
from ..common import writeFile