Importing custom library from different folder path in python - python

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!

Related

How to add "src" to PATH in a PyCharm Project so it doesn't have to appear in all the imports?

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?

ModuleNotFoundError when working with multiple folders

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!

Jython can't detect python files inside a subdirectory

I have already tried appending the path to sys.path, but that doesn't solve the problem either. The import works fine with my Python36 interpreter.
My directory structure is:
project (dir)
:main.py
:=> Algorithms (sub-dir)
:==> AlgoOne.py
My code in the entry file which is one level above the Algorithms dir is:
#main.py
from Algorithms.AlgoOne.py import ClassOne
Any ideas why this could happen, or how I instruct jython to read the directory as a module like Python36?
Thanks

Finding import path in pycharm

I am using pycharm version 3.4 on ubuntu and have a project with the following structure:
~/project/src/python/utils/sub1/sub2/sub3/my_code.py
the utils folder also contains an __init__.py file which offers a number of utility functions. I want to include some of them, but it wouldn't find it:
from project.src.python.utils import read_utf8
I followed this post, which seemed to discuss the same problem:
PyCharm can't find the right paths if I open a directory that is not the Django root
But changing ~/project to a "source" folder didn't help. This isn't a typo on my side, as it should at least find "project" when trying to import something from it, but
import project
also gives my an "unresolved reference" error.
edit: I should add that I cannot change the code, as it is a shared project. I need that exact import line to work on my machine. My counterpart uses eclipse, were it seems to be easier to add a path to additional code.
It looks like the source isn't in your PYTHONPATH which is why you're unable to import it.
python only looks in certain places for py/pyc/pyo etc. files and modules to import (the places are the paths listed in sys.path). You can add custom places for it to look with the PYTHONPATH environment variable, or in PyCharm under preferenes > project interpreter > configure interpreters, and then adding any paths in the paths section.
I can't tell which folders are actually modules but from the names of the folders I would guess only utils. If that's the case add the path /home/ceca/project/src/python to the list of paths in PyCharm and in your code from utils import read_utf8

Multiple user created modules from a directory not in PATH using just command window (Anaconda 3)

Up to this point I have organized my projects in such a way that everything I'm working on is in the same folder, so to play around/debug I have just launched Python from that folder like this:
C:\Users\Username\Dropbox\Projects\MyShinyProject>Python
>>>
However, I want to start organizing things better. I have created some "Utilities" classes that I expect I'll use over and over again. So they should be in their own folder.
So now, say I have a Projects folder (in Windows) with lots of subfolders of things I have been working on:
Projects
Sandbox
Sandbox1
Sandbox2
MyUtilities
__init__.py
Utility1.py
MyShinyProject
__init__.py
ImportantClass.py
I would like to be able to go into the command prompt and use classes/functions from both the MyUtilities folder and from the MyShinyProject folder. However, if I launch Python from inside MyShinyProject, I don't have access to MyUtilities (or vice versa). I've tried doing a relative import like this:
>>>import ..MyUtilities.Utility1
But that doesn't work:
import ..MyUtilities.Utility1
^
SyntaxError
If it matters: I don't use an IDE. I just use Notepad++ and the command prompt. Also, I added the __init__.py files to the folders because I read somewhere you're supposed to do that when you make modules, but I don't understand how to get all of this working correctly, or if I'm even close to doing it right.
I also tried adding my Projects folder to the PATH variable in the Windows environment table, but that doesn't seem to work. Even after adding it importing doesn't work, and when I do this:
import sys
for x in sys.path:
print(x)
...the folder I added to PATH does not appear (I tried adding it to the beginning and the end).
How can I use several of my user created modules together using the command prompt to import them?
Assuming you have __init__.py in your Projects folder, in the console you can do this:
import sys
sys.path.append("C:\Users\Username\Dropbox\Projects")
import Projects.MyUtilities.Utility1
Or if you want to add your desired directory permanently to the python path, you can append your directory to the value of the environment variable called PYTHONPATH.

Categories

Resources