How to make Python try to load modules from local folder? - python

I have a Python package that has CLI interface. I would like to work on the package locally and add some features. What is the best way to setup the development environment so that the cli loads the modules defined in the local folder?
.
├── __init__.py
├── cli.py
├── diff_augment.py
├── diff_augment_test.py
├── lightweight_gan.py
└── version.py
python cli.py
ModuleNotFoundError: No module named 'lightweight_gan.diff_augment'; 'lightweight_gan' is not a package
If I start up cli.py (python cli.py) it is going to try to load all the modules from the virtualenv instead from the local folder. Is there a way to make Python load the modules first from the local folder?

Related

How can I set all subdirectories of a directory into $PYTHONPATH?

Running vscode from my machine to remote server,I am trying to import some modules from parent directories. What is the correct way to import the modules from run.py? Do I have to setup a configuration json file in the .vscode folder? Since I have a lot of modules the solution with the os package is not very helpful. Is there a way of importing all the custom modules from the current working directory?
Thank you
.
├── folder1
│ └── module1.py
├── folder2
│ └── module2.py
└── folder3
└── run.py
VSCode 1.72.0
Ubuntu 22.04.1 LTS
You may use the command line as follows:
python3 -m folder3.run
The official docs here.
Or another very detailed answer here.

Python Library - Module Not Found

I'm making a python library and i want to be able to run the code i'm developing, in another folder i have a python file, but I get the error: ModuleNotFoundError: No module named
this is my folder structure
.
└── project
└── library_directory
├── __init__.py
└── main.py
└── examples_directory
├── __init__.py
└── code_directory
├── __init__.py
└── test.py
init.py from library_directory
from library_directory.main import Class
test.py file
from library_directory import Class
when I run test.py file it says: ModuleNotFoundError: No module named 'fpdf_table'
if i put test.py file at project level this configuration of init and test works, but i want to run the test.py in the code_directory because i will have a lot of files and don't want 15+ single files at project level
.
└── project
└── library_directory
├── __init__.py
└── main.py
└── examples_directory
├── __init__.py
└── code_directory
├── __init__.py
└── test.py
i already tried absolute and relative imports but they don't work
Im not sure if this is the correct solution, but you can try this:
In your test module:
import sys
sys.path.insert(0, '/Your_project_root_path'
Now can access to the packages in the root directory.
I took this solution from here.
Your library_directory folder is a Python package. Within the package, each .py file is a module.
In your library_directory/init.py file, insert the line from .main import Class. Now you have a package called "library_directory", which has a module called "Main", and a class called "Class".
Now, in your environment variables, create a user variable called PYTHONPATH and add to this, the path to your project directory.
When you import in your project file, you should import using the structure: from package.module import class, or in your case: from library_directory.main import Class. The python interpreter will be able to find the package due to being in your PYTHONPATH and the init file directory means Python will recognise it as a package.
(You may wish to rename "library_directory" to be a bit more project specific)

Python not finding local module folders

So I have a project that is layed out as followed:
.
└── project
└── src
├── app.py
└── connector
├── update.py
└── connect.py
└── transform
└── dataframes.py
In the project, app.py imports from the transform module which imports from the connector module. However,connector is never found. I imagine this is due to the folder structure.
Does anyone know a work around for this?
Python is bit different than java. To identify a folder as a module you need to have empty __init__.py file inside that folder.
if that file is doesn't exist it is going to throw error that it's not able to find module.
Other options is use pycharm and rather than creating a folder, right click on choose package.

How do I structure my python project such that my test files can import the packages in the root folder?

I would like to integrate pytest into my workflow. I made a following folder structure:
myproject
├── venv
└── src
├── __init__.py
├── foo
│ ├── __init__.py
│ └── bar.py
└── tests
├── __init__.py
└── test_bar.py
I would like to be able to import the namespace from the foo package so that I can write test scripts in the tests folder. Whenever I try to run pytest, or pytest --import-mode append I always get the following error:
ModuleNotFoundError: No module named 'foo'
I found this similar question here but adding the __init__.py files to the tests and the src folder does not solve the issue.
Does this have to do with the PYTHONPATH system variable? This folder structure works perfectly if I run the __main__.py from the src folder, but fails when I want to use pytest. Is there a way to do this without having to mess with PYTHONPATH or are there automated ways to edit the system variable?

Project module not found outside virtualenv

My project has basically an architecture like this:
src
├── __init__.py
├── main.py
└── core
├── __init__.py
├── module1.py
└── module2.py
All __init__.py files are empty, in main.py I have a from src.core.module1 import stuff and I am running main.py from the src folder.
When I'm running it from my project virtual environment everything works fine, but outside of the virtual environment I have an ImportError: no module named src.core.module1. I cannot understand why, because this module is in the project, not related to the Python environment packages...
(Windows / Python 2.7.14)
Try from core.module1 import stuff. You should not import src since you are in that location with your main.py already.

Categories