I got a question/problem related to how to use init.py file for a project im working on. Im trying to run train_pipeline.py, which is located at
packages/cnn_modality_clf/cnn_modality_clf/train_pipeline.py
This file uses scripts that are located in the same folder, and also files that are within folders.
My imports (within train_pipeline.py
from cnn_modality_clf.pipeline import pipe
from cnn_modality_clf.config import config
from cnn_modality_clf.preprocessing import data_management as dm
from cnn_modality_clf.preprocessing import preprocessors as pp
However, when I run this train_pipeline file, I get a ModuleNotFoundError: No module named cnn_modality_clf and the error is on from cnn_modality_clf.pipeline import pipe
I have worked on previous projects (not that many) where using the init.py file will solved this issue. However, I have added this file into every folder. But that doesnt seem to work out.
My project structure looks like this:
I want to be able to use the python files that are within the same path, or that are within a folder (cnn.modality_clf.pipeline import pipe)
*Im using anaconda environment, and im using python 3.7.0
Any suggestions on how to overcome this problem?
Thanks in advance!
Related
My PyCharm project contains folders such as src, data, notebooks etc.
Within my notebooks folder, I have a small notebooks.py file which contains:
import sys
sys.path.append('../src')
So within my notebooks, I do the following:
import notebooks
import preprocessing.some_module
However, all my code is naturally within src and is full of imports such as:
from src.preprocessing.some_module import SomeClass
So when I try to import something within the notebooks, I get errors telling me src is not found.
If I set the content root at src within the Project Structure, all the other folders simply disappear so it's not really a solution.
So, is there a way for me to be able to refer to my project modules simply as preprocessing.some_module rather than src.preprocessing.some_module so that I can run my code smoothly both using PyCharm and on AWS?
Also, I have a bunch of constants in my code such as:
SOME_FOLDER = '../../data/some_folder'
These work fine for now, while I run from PyCharm but I fear this relativity might also get messed up when I start running the code from Jupyter. What is the best way to define these paths so they work no matter what?
I am doing some multiprocessing work on python which isn't doable from a jupyter notebook. I have written the required functions in a .py file and I'm now importing it into my notebook.
How do I import this directly from the same folder without specifying the directory of where the file is in? This needs to be run-able on different PCs when sent to other people and thus I dont want to append the system path for my computer specifically.
import sys
#sys.path.append to get
sys.path.append()
from map_reducer import *
Thank you.
You can import them with:
from FileNameWithOutTheDotPy import *
And then just run them like normal:
FunctionThatDoesStuff(var1,var2)
Note: They do need to be in the same directory for this to work.
I am having an issue with python not being able to find or import my module. I am unsure how to fix this problem. My py file I'm trying to get my import into is in 'main' folder, the file I'm trying to import is one level up. My program is a package:
Error I'm getting
models.py
Trying copying your .py file to site-packages folder present in the Python folder and then running your python file from command prompt(python -m <your_file_name>.py)/python launcher/IDE. In case you are still getting the error then share your code as well so that people can see.
I have the following folder structure:
A/fold1/MyLib/package/
in which I have an __init__.py file and a file with the functions I need, let's call it file.py.
When I work from this path:
A/fold1/myProject/script.py
I try to import it but it looks like this library can't be found.
I have already worked, and it was fine when I had a structure like this:
A/fold1/myProject/package/file.py
where package contained also the __init__.py file.
I also got suggestions from the Spyder IDE.
What I used, to make it work was to import like this:
if packagePath not in sys.path:
sys.path.append(packagePath)
import package.file as f
But currently it doesn't work, and I don't even get the suggestions from the Spyder IDE.
Is there a way to import from the specified structure?
I wanted a folder with all my functions, and if possible to have it in this folder and not between the hundreds of libraries of python.
Thanks!
I am trying to import a python program from within another python program, however these are not in the same directories, and so I am using this code:
import sys
sys.path.append("C:/Users/Name/Desktop/Project")
import Maths
This works, and it opens the program. However, the problem I am having with this is that the program I am importing relies on some images and files that don't seem to get loaded (which prevents the program from running properly). These files are placed in
C:/Users/Name/Desktop/Project/resources
What I've Tried
I have tried placing my program that I am importing into the same directory as the files it relies on, but this came with the same error as shown here:
couldn't open "resources/bg.png": no such file or directory
So my question is - how can I fix the issue I describe above?
One possible solution is to turn Maths module into an import package. Put it inside your working folder, in a subfolder. Put an empty __init__.py in that subfolder to make Maths a package. You can now import it as usual from any module that is started from the working folder. To solve the error you'll have to abstract the resources location inside the Maths code. Try using pkgutil.get_data(package, resource) to get the contents of those files.
See pkgutil docs, in the page bottom.