I have a directory structure for my Flask-Dash app:
I am following this tutorial: https://dash.plotly.com/integrating-dash
to combine one or more Dash apps with a Flask app.
But in wsgi.py, it could not find a reference for dash_app2.py until I renamed it using Roman numerals dash_appII.py.
Is there a naming convention in Python where two files in the same package or directory cannot have similar names like dash_app1.py and dash_app2.py?
It could not even find a reference to dash_app2.py in the __init__.py file in that directory which now looks like:
from .dash_app1 import dash_app1
from .dash_appII import dash_app2
I don't really want to use Roman numerals.
`
Are you importing dash_app1 somewhere else in your app? I've found that the explicit relative import can sometimes cause namespace collisions if you use it after importing the packages somewhere else with a non-explicit import. Worth a shot to take a look or simply remove the . explicit and give it a go. All else hold equal, you shouldn't have issues importing the numeric versions of your files.
Related
There is a question like mine in the link below:
creating python package with multiple level of folders
However, after following the answers to the question and trying some other things that I thought might work I did not succeed.
I have created a working package with a number of functions here:
https://github.com/aaronengland/prestige
In the prestige directory is an init.py file containing some classes and functions. I have a class named preprocessing and I can call any of the functions from that class using:
from prestige import preprocessing as pre
And then (for example):
pre.Binaritizer()
However, I want to be able to import those functions using:
import prestige.preprocessing as pre
Using the first link (above) I was unsuccessful in doing this. I feel like it should be a simple solution, but for some reason I have not been able to get it to work. Can someone please show me how to make this possible? Thank you in advance!
I was able to solve the problem by organizing the file structure as follows:
prestige
setup.py
init.py
general.py
preprocessing.py
setup.py was set up as I normally do, general.py contains functions/classes, and preprocessing.py contains functions/classes. The init.py file contains 2 lines of code:
from .preprocessing import * and from .general import *
So, I did not create new directories, I just divided my functions into separate .py files and imported them into my init.py file.
Now, I am able to import functions using, for example:
from prestige.preprocessing import Binaritizer
Hopefully this helps someone in the future with a similar question.
The package can be accessed here.
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.
I'm trying to rearrange my project files.
I'm having some hard time figuring out what is the correct way to name files and folders. I'm relying on PEP8. I see that I should avoid having underscores in file/folder name. But if I have a folder like functionalcoverage it seems weird and feels like I should call it functional_coverage instead and the same goes for files(securecopy.py and secure_copy.py).
In addition, I'm not sure if I should adhere to PEP8 regarding folders/files that aren't python(a folder containing a bunch of txt files for example)
Its prety much what you prefer, a lot of people use underscore, PEP8 is a guide you can follow or not, the important is to be consistant with your choice read more here
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
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.