I am trying to work from csv files located inside of a django app. I am trying to load the file using pandas like: pd.read_csv("...") without success, I keep getting an error.
Here is what the directory tree looks like:
├── __pycache__
│ ├── forms.cpython-36.pyc
│ ├── models.cpython-36.pyc
│ ├── views.cpython-36.pyc
│ └── urls.cpython-36.pyc
├── apps.py
├── files
│ ├── t1.csv
│ ├── t2.csv
│ ├── t3.csv
│ ├── t4.csv
│ └── parametre.csv
├── finished_apps.py
├── forms.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-36.pyc
│ ├── 0002_remove_carriers_carriersheet.cpython-36.pyc
│ ├── 0003_auto_20211021_1200.cpython-36.pyc
│ ├── 0004_auto_20211021_1203.cpython-36.pyc
│ └── __init__.cpython-36.pyc
├── models.py
├── views.py
├── templates
│ ├── add_carrier.html
│ ├── base.html
│ ├── delete_carrier.html
│ ├── delete_carrier_confirmation.html
│ ├── _carrierdetails.html
│ ├── _carrierlist.html
│ ├── simulation.html
│ └── update_carrier.html
└── urls.py
I have tried the following inside of views.py
df = pd.read_csv("/files/t1.csv") #not working
df = pd.read_csv("./files/t1.csv") #not working
df = pd.read_csv("t1.csv") #not working
df = pd.read_csv("../files/t1.csv") #not working
I have also tried doing that:
from files import t1
the errors that I am getting are as such:
No such file or directory (/file/t1.csv) #for example
cannot import name 't1'
that's not working either.
I am now wondering whether is it possible to import a file this way or I am missing something obvious here!
Get the path to views.py from the __file__ variable and use that to find the path to your CSV:
import os
import pandas as pd
path = os.path.join(os.path.dirname(__file__), 'files/t1.csv')
df = pd.read_csv(path)
Related
ImportError while loading conftest '/home/rohit/flask/src/tests/conftest.py'.
conftest.py:6: in <module>
from mycode import create_app
E ModuleNotFoundError: No module named 'mycode'
when running pytest form the src folder above mycode folder, it gives ModuleNotFoundError
The directory structure is as follow
├── mycode
│ ├── auth
│ ├── auth.py
│ ├── db_insert.py
│ ├── db.py
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── auth.cpython-37.pyc
│ │ ├── db.cpython-37.pyc
│ │ ├── db_insert.cpython-37.pyc
│ │ └── __init__.cpython-37.pyc
│ ├── README.md
│ ├── schema.sql
│ └── templates
│ ├── auth
│ │ ├── login.html
│ │ └── register.html
│ └── base.html
└── tests
├── conftest.py
├── data.sql
├── __pycache__
│ └── conftest.cpython-37-pytest-5.4.1.pyc
└── test_factory.py
Your __init__.py file needs to go in the folder named mycode. If the above is already true then try the following.
Rename __init__.py to mycode.py
You can see here for more solutions that may solve your problem.
I'm trying to import a view from one app to another in my project.
When using this:
from ..from ..KnownLocation.views import KnownLocationView
I get the following error:
ValueError: attempted relative import beyond top-level package
When trying to use:
from triangulationapi.KnownLocation.views import KnownLocationView
It's raising the following error.
ModuleNotFoundError: No module named 'triangulationapi.KnownLocation'
my Project tree:
├── find_second_gdt
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── second_GDT_finding
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── __init__.py
├── KnownLocation
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── manage.py
├── requirements.txt.
└── triangulationapi
├── asgi.py
├── __init__.py
├── __pycache__
├── settings.py
├── urls.py
└── wsgi.py
And, what's the diffrence between using .. and project.app.view...
I thought it is the same up until now.
Try this:
from .KnownLocation.views import KnownLocationView
I am trying to call a function from a python file that is located outside the django project directory from views.py. I have tried importing the python file but running the django server says "no module named xxxx".
Tree structure is given below.
├── auto_flash
│ ├── __init__.py
│ └── test.py
└── fireflash
├── detection_reports
│ ├── admin.py
│ ├── admin.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── __pycache__
│ │ └── __init__.cpython-35.pyc
│ ├── templates
│ │ └── detection_reports
│ │ └── home.html
│ ├── tests.py
│ ├── views.py
│ └── views.pyc
├── fireflash
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ └── settings.cpython-35.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── ui_cgi.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── __init__.py
└── manage.py
Project name here is "fireflash" and there is an app in that project named "detection_reports". There is another directory named "auto_flash" that has same location as "fireflash". What I want to do is to import test.py file from "auto_flash" in detection_reports.views and call a function from views. Importing like this "from auto_flash import test" throws error "no module named auto_flash".
I have tried all of the above mentioned solutions, but the cleaner solution was to append the path for auto_flash to syspath and the issue was resolved. Thanks to all of you for the efforts.
Move auto_flash to fireflash
In detection_reports add from auto_flash import test.py
I am writing a custom Django module but I seem to have something wrong. I cannot import a class that lives in a certain file. I get the error
ValueError: Unable to configure handler 'admins': Cannot resolve 'myPackage.handlers.MyHandlerClass': No module named handlers
This is the directory structure. I believe I can import views and models with no problem.
myPackage
├── CHANGELOG.rst
├── myPackage
│ ├── handlers .py
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── views.py
│ └── views.pyc
├── myPackage.egg-info
│ ├── dependency_links.txt
│ ├── PKG-INFO
│ ├── requires.txt
│ ├── SOURCES.txt
│ └── top_level.txt
├── MANIFEST.in
├── README.rst
├── requirements.txt
└── setup.py
There is a space in the filename of handlers .py, so python can't find a module names handlers. Obviously the easiest fix is to correct the filename, but for anyone actually wanting a space in the filename, import name with spaces is a syntax error, so the only way to import such a name is using __import__. But this is really a very bad idea.
I've the following project setup
....
├── lira
│ ├── __init__.py
│ ├── admin.py
│ ├── ajax.py
│ ├── authentication.py
│ ├── context_processors.py
│ ├── fencoder
│ │ ├── __init__.py
│ │ ├── encoder.py
│ │ ├── ffmpeg_commands.py
│ │ ├── frames.py
│ │ ├── utils.py
│ │ └── video.py
│ ├── models.py
....
And when I try to import from lira.fencoder import encoder I get an error ImportError: cannot import name encoder.
What is wrong with the above project setup though this morning it was the same and it worked?
Sultan
If you want to debug import problems, sometimes a simple launch of
python -v -m path/to/python/module/to_start
will help you, it will show you all the imports done by the python interpreter and help detect cyclic imports.