How do I use modules in Django? - python

I'm trying to display a graph on a web page. I can get the graph to show up with a simple example that only uses functions defined within the function. However, I want to be able to expand it further. In my original code, I have one main 'graphing' function that uses the functions of other modules so that it stays organized. When I try to import these modules, which exist as files within the Django app folder, it says there is no module with that name. How do I fix this?
Error: File "/Users/andrewho/Desktop/website/charts/views.py", line 48, in <module>
import graphing
ImportError: No module named 'graphing'
I clearly have a file in the app folder called graphing.py, so why does it give me this error?

use " from 'where' import 'what'"
so may be like "from . import graphing" if its in the same directory

Related

'somewhere' gives error : No module named 'where' for my django app

When I import ( like below after doing pip install somewhere ) handle_uploaded_file from 'somewhere library on my linux
machine.)*
from somewhere import handle_uploaded_file
Getting following error when trying to run my django application.)-
Seems I scared previous person that provided a correct answer as it was deleted, but
I'm gonna guess you are following this example. As you can see, there is a clear comment that it is supposed to be imaginary function, i.e. one that you need to implement yourself if you want to use it in the actual code. It's not supposed to refer to somewhere module which does not offer any function of that name.
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file

Jupyter Notebook: Newly added functions not accessible

similar to this issue and this topic related article, I have created my own
package within Jupyter Notebook. I can successfully access module content after the first import of a python file.
However, whenever I'd like to add a new function to the python file, I am unable to access it then within my notebook.
I've tried the following:
- adjust and save the python file online
- delete the old python version and upload a new one
The only thing working was to upload the python file with a different name. But this is not really what I want to achieve :D
Anyone here having an idea how to add new functions with direct access ability?
This is how I import my modules:
import os
import sys
sys.path.insert(0, os.path.abspath('/home/ubuntu/jupyter/src/..'))
from src.parsing import general
general. <-- function list popping up
When you are in a cell with the code/function you want to use, press Shift + Enter to be able to access that code in the next cell.
I solved the issue. It was rather obvious. You need to restart the kernel, so that the newly added methods can be accessed.
Just rerunning the import cell is not sufficient.

"No module named" error for modules in one folder and valid PYTHONPATH

I have the following structure in my project:
Python-auto-tests
Business Layer
Yandex
__init__.py
Authorization.py
Yandex_requests.py
Documents
venv
In "Authorization.py" file I have only 1 method
In "Yandex_requests.py" I trying to import "Authorization.py" module:
import Authorization
But I get following error:
"No module" named Authorization
My PATHONPATH environment variable is set to project path:
C:\Users\anduser\Python-auto-tests
Also I check my sys.path and it looks fine, my folders are here:
C:\Users\anduser\Python-auto-tests\venv\Scripts\python.exe "C:\Users\anduser\Python-auto-tests\Business Layer\Yandex\Yandex_requests.py"
C:\Users\anduser\Python-auto-tests\Business Layer\Yandex
C:\Users\anduser\Python-auto-tests
C:\Users\anduser\AppData\Local\Programs\Python\Python37-32\python37.zip
C:\Users\anduser\AppData\Local\Programs\Python\Python37-32\DLLs
C:\Users\anduser\AppData\Local\Programs\Python\Python37-32\lib
C:\Users\anduser\AppData\Local\Programs\Python\Python37-32
C:\Users\anduser\Python-auto-tests\venv
C:\Users\anduser\Python-auto-tests\venv\lib\site-packages
C:\Users\anduser\Python-auto-tests\venv\lib\site-packages\setuptools-40.8.0-py3.7.egg
C:\Users\anduser\Python-auto-tests\venv\lib\site-packages\pip-19.0.3-py3.7.egg
Can you help me solve this issue? I just can't understand why Python doesn't see my module.
In the official Python documentation there is example how to import module from the same folder and I do the same.
You have two problems here. Let's tackle the one you are aware of first.
You say you have set PYTHONPATH=C:\Users\anduser\Python-auto-tests. As such, any imports you make must be relative to that path. For example, instead of import Authorization, you have to do from Business Layer.Yandex import Authorization.
Your second problem, and I think you are unaware of it, is Business Layer. Using default import methods, Python does not handle spaces in directory of module names. (Note that it also does not handle hyphens and several other special characters). You should change that folder to something like BusinessLayer or Business_Layer. Refer to Package and Module Names from the PEP8 -- Style Guide for Python Code for more information on naming conventions for different Python constructs.
Ultimately, as long as your PYTHONPATH remains the same, the import should be written as something like from BusinessLayer.Yandex import Authorization.

Error at importing module

I have been writing some code and used it as a module. This was working properly, until this morning one of the modules would not load.
I have multiple programs in one direction. most of them load, except for two.
I have tried importing it using:
import company.a
from company import a
Both does not work.
However the following things both do work:
import company.b
from company import b
Both are in the same folder, containing __init__.py.
Can someone help me with this?
For some reason, it did work when I made an exact copy of the company folder, and named it company2.
the following does work:
from company2 import a
while it is the exact same file, in an folder containing exactly the same, with a different name.
Solved for now, although I am curious why changing the name of the folder made the difference

Using a class from another folder in the current code

My apologies for a seemingly easy question, I'm new to using classes in Python.
I am using Pycharm and my folder structure looks as follows:
The folder constant-contact-python-wrapper has a few classes defined under __init.py__ and restful_lib.py (I got this library from github). I would like to use these classes in the file Trial.py contained in ConstantContact folder. I am using the following code but it is not able to import the class.
import sys
sys.path.append('C:\\Users\\psinghal\\PycharmProjects\\ConstantContact\\constant-contact-python-wrapper')
import constant-contact-python-wrapper
API_KEY = "KEY" #not a valid key
mConnection = CTCTConnection(API_KEY, "joe", "password123")
Would someone please be able to point me to the right direction?
Part of the problem that you're trying to rectify is that you have two libraries that are together in the same scope, even though it doesn't look they necessarily need to be.
The simplest solution would be to simple put constant-contact-python-wrapper in the ConstantContact folder under a new folder for code you will be importing that you yourself did not write. This way your project is organized for this instance and for future instances where you import code that is from another library
Ideally the folder structure would be:
ConstantContact
|___ ConstantContact
|____ExternalLibraries #(or some name similar if you plan on using different libraries)
|___constant-contact-python-wrapper
Using the above model, you now have an organized hierarchy to accommodate imported code very easily.
To facilitate easy importing you would additionally setup the following:
1.Create init.py file in ExternalLibraries. The contents would be the following:
from constant-contact-python-wrapper import #The class or function you want to use
This will facilitate imports and can be extended for future libraries you choose to use.
You can then use import statements in your code written in the ConstantContact folder :
from ExternalLibraries import #The class or function you chose above
if you have multiple classes you would like to import, you can seperate them in your import statement with commas. For example:
from Example import foo,bar,baz
Since the init.py file in ExternalLibraries is import all functions/classes directly, you can use them now without even having to use dot syntax (ie. library.func).
Sources and further reading:
"all and import *" Can someone explain __all__ in Python?
"Python Project Skeleton" http://learnpythonthehardway.org/book/ex46.html
"Modules" http://docs.python-guide.org/en/latest/writing/structure/#modules
constant-contact-python-wrapper and ConstantContact are unrelated packages for python. Create a __init__.py in the same directory as manage.py and it should work as expected.

Categories

Resources