Conda doesn't see a module in the same directory - python

When I try to run a script in conda environment it gives me a ModuleNotFound error
(ldm) C:\Users\Иван\Documents\git\stable-diffusion>python scripts/txt2img.py --prompt "a photograph of an astronaut in space" --plms
Traceback (most recent call last):
File "scripts/txt2img.py", line 17, in <module>
from ldm.util import instantiate_from_config
ModuleNotFoundError: No module named 'ldm'
Here are the txt2img.py's lines causing the trouble
from ldm.util import instantiate_from_config
from ldm.models.diffusion.ddim import DDIMSampler
from ldm.models.diffusion.plms import PLMSSampler
But the main points are:
There is a folder called "ldm" containing everything needed
There are no complains from the compiler in the actual code editor
What I'm sure of:
The txt2img.py's directory is C:\Users\Иван\Documents\git\stable-diffusion\scripts\txt2img.py and the ldm's directory is C:\Users\Иван\Documents\git\stable-diffusion\ldm
Anaconda is added to my PATH environment variable
I'm trying to make it work in VSCode terminal (I set it up to run conda) but it's absolutely the same in the separate Anaconda Prompt
The problem does not occur with other projects
Is there a way to fix this?

The problem got resolved with typing in the following:
pip install -e .
I think it just installs all the packages from the project folder

As a quick solution try the following:
cd C:\Users\Иван\Documents\git\stable-diffusion
C:\Users\Иван\Documents\git\stable-diffusion> python scripts\txt2img.py --prompt "a photograph of an astronaut in space" --plms
In VSCode, do the following:
Add 'C:\Users\Иван\Documents\git\stable-diffusion' to your workspace
Open VSCode Terminal (ctrl + ~)
Follow the steps from here
Run your script
python scripts\txt2img.py --prompt "a photograph of an astronaut in space" --plms
Update based on comment:
You need to add an empty __init__.py inside all your folders. Refer here

Related

Python3 ModuleNotFoundError when running from command line but works if I enter the shell

I think I'm missing something obvious here. I cloned this repo, and now have this directory structure on my computer:
When I try to run python baby_cry_detection/pc_main/train_set.py, I get a ModuleNotFoundError.
Traceback (most recent call last):
File "baby_cry_detection/pc_main/train_set.py", line 10, in <module>
from baby_cry_detection.pc_methods import Reader
ModuleNotFoundError: No module named 'baby_cry_detection'
However, if I type python and enter the interactive shell and then type the command
from baby_cry_detection.pc_methods import Reader
it imports just fine with no error. I'm completely baffled. I'm using a virtualenv and both instances are using the same python installation, and I haven't changed directories at all.
I think sys.path could be the reason that the module is not found when python command is executed. Here is how we can check if that is indeed the case:
In the train_set.py file, add import sys; print(sys.path). Looking at the error, the path may contain /path/to/baby_cry_detection/baby_cry_detection/pc_main. If that is the case, then we have found the issue which is that baby_cry_detection.pc_methods will not be found in the directory that sys.path is looking into. We'll need to append the parent baby_cry_detection directory to sys.path or use relative imports. See this answer.
The reason that python prompt successfully imports the module could be because the prompt is started in the correct parent directory. Try changing the directory to baby_cry_detection/pc_main/ and try importing the module.

How to fix ModuleNotFound error in python building?

I trying to build the ungoogled chrome source from github. I was following the instructions in the link below, but I really do not know how to continue.
I installed python 2.7 and 3.7, set them in the PATH.
Used the git clone command and jumped the replace comands and the git checkout too, because I didin't got them.
So, I tried the "py build.py" command and got this error.
C:\Users\aquasp\ungoogled-chromium-windows>py build.py
Traceback (most recent call last):
File "build.py", line 24, in <module>
import buildkit.config
ModuleNotFoundError: No module named 'buildkit'
Is there any suggestions?
This are the commands that I was folowing:
git clone --recurse-submodules https://github.com/ungoogled-software/ungoogled-chromium-windows.git
# Replace TAG_OR_BRANCH_HERE with a tag or branch name
git checkout --recurse-submodules TAG_OR_BRANCH_HERE
py build.py
py package.py
https://github.com/ungoogled-software/ungoogled-chromium-windows
I'm assuming you are on a windows machine. Try running the dos 'which' command with an argument of 'buildkit' as follows:
which buildkit
The output will be the search of directories in the path variable of the windows machine as follows:
which: no buildkit in (/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1
etc...
'buildkit' is not found on path or the python.exe is not on the path.
Try 'which python' or 'which py' to test for python.exe or py in the machine path.
if python.exe is found output is as follows:
/c/Program Files/python/python
or if not found
which: no python found in (/c/WINDOWS/system32: etc...
Last but not least, add buildkit or python.exe or py to your machines path variable as follows:
set path=%path%;plus\new\path
C:\Users>echo %path%
C:\WINDOWS\system32;plus\new\path

ModuleNotFoundError: No module named 'object_detection'

i try to train.py in object_detection in under git url
https://github.com/tensorflow/models/tree/master/research/object_detection
However, the following error occurs.
ModuleNotFoundError: No module named 'object_detection'
So I tried to solve the problem by writing the following code.
import sys
sys.path.append('/home/user/Documents/imgmlreport/inception/models/research/object_detection')
from object_detection.builders import dataset_builder
This problem has not been solved yet.
The directory structure is shown below.
~/object_detection/train.py
~/object_detection/builders/dataset_bulider.py
and here is full error massage
/home/user/anaconda3/lib/python3.6/site-packages/h5py/init.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated.
In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Traceback (most recent call last):
File "train.py", line 52, in
import trainer
File"/home/user/Documents/imgmlreport/inception/models/research/object_detection/trainer.py", line 26, in
from object_detection.builders import optimizer_builder
ModuleNotFoundError: No module named 'object_detection'
how can i import modules?
Try install Tensorflow Object Detection Library Packaged
pip install tensorflow-object-detection-api
Cause of this error is installing object_detection library, So one of the solution which can work is running the below command inside models/research
sudo python setup.py install
If such solution does not work, please execute the below command one by one in the directory models/research
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
sudo python setup.py install
I hope this will work. I also faced the same problem while creating model from export_inference_graph.py. It worked for me.
You need to export the environmental variables every time you open a new terminal in that environment.
Please note that there are are back quotes on each of the pwd in the command as this might not be showing in the command below. Back quote is the same as the tilde key without pressing the shift key (US keyboard).
From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
try this:
python setup.py build
python setup.py install
There are a number of modules in the object_detection folder, and I have created setup.py in the parent directory(research folder) to import all of them.
from setuptools import find_packages
from setuptools import setup
REQUIRED_PACKAGES = ['Pillow>=1.0', 'Matplotlib>=2.1', 'Cython>=0.28.1']
setup(
name='object_detection',
version='0.1',
install_requires=REQUIRED_PACKAGES,
include_package_data=True,
packages=[p for p in find_packages() if p.startswith('object_detection')],
description='Tensorflow Object Detection Library',
)
You did have "sys.path.append()" before you imported the object detection, so I am surprised that you are facing this error!
Please check that the path you have used in sys.path.append() is right.
Well, the only and obvious answer for the error is that the path of the module is not added properly.
Besides the various ways mentioned here, here is a way in which you can add the "object_detection" path permanently to the PYTHONPATH variable.
If you are using a Linux system, here is how you would go about it:
Go to the Home directory. Press Ctrl + H to show hidden files. You will see a file called ".bashrc". Open this file using a code editor (I used Visual Studio).
In the last line of .bashrc file, add the line:
export PYTHONPATH=/your/module/path:/your/other/module/path:your/someother/module/path
Then press "save" in the code editor. Since ".bashrc" is a "Read-only" file the editor will throw a pop-up saying the same. Also in the pop-up there will be an option that says: "Try with sudo". Hit this button and now you are good to go.
All your modules are now permanently added to the PYTHONPATH. This means that you need not run sys.path.append every time you open your terminal and start a session!
Below is the screenshot with no error when I followed the said steps:
Try this. I hope it helps.
And finally, If you've followed all the steps here and are at your wit's end...make sure the file that you're running (the one with your source code in it ya know), isn't named object_detection.py - that would preclude it being searched for as a module.
Certainly I've never done anything like this that led me to add an embarrassing answer on Stack Overflow...
I had to do:
sudo pip3 install -e . (ref)
sudo python3 setup.py install
System:
OS: Ubuntu 16.04, Anaconda (I guess this is why I need to use pip3 and python3 even I made virtual environment with Pyehon 3.8)

how to provide 'make directory as source root' from pyCharm to terminal

I have some problems with migrating to production:
cabox#box-codeanywhere:~/workspace/PEP$ python ./dev_scrapers/jordan.py
Traceback (most recent call last):
File "./dev_scrapers/jordan.py", line 3, in <module>
from utils import create_entity, create_id, custom_opener
ImportError: No module named utils
i have used pyCharm with button 'make directory as source root'
how to execute such command in terminal?
You should add your source root directory to PYTHONPATH:
export PYTHONPATH="${PYTHONPATH}:/your/source/root"
You can set the environment variable PYTHONPATH within the terminal as suggested by the accepted answer. This has to be done every time you start a terminal. In the case you use a virtual environment, you can place the variable assignment, like export PYTHONPATH="/your/source/root", in the file venv/bin/activate. Where venv stands for the name of the virtual environment directory.
I was able to set directory as root (and fix the unresolved import issue) doing the following:
from pycharm>Settings>Project>Project Structure select your project and from the file tree, select your django project directory then click the blue folder Source button to define the directory as your source.

ImportError: No module named stack

I have a code in python that I have been working on and it builds and runs very well on my pc (Windows). I had to run the same code on my other machine which runs ubuntu,so I had to install all the packages on prior to runing the code. The problem is I ran into this error which I couldn't figure out. The error is triggered by one of the installed packages.
from qalsadi import analex
File "/usr/local/lib/python2.7/dist-packages/qalsadi/analex.py", line 14, in <module>
import pyarabic.araby as araby # basic arabic text functions
File "/usr/local/lib/python2.7/dist-packages/pyarabic/araby.py", line 28, in <module>
from stack import *
ImportError: No module named stack
I used the following command, "sudo pip install pyarabic", to install it. However, still the file stack.py doesn't exist among it's files. I searched in the folder /usr/local/lib/python2.7/dist-packages/pyarabic. The folder contains the following: araby.py and init.py and the coresponding pyc files only. I'v insalled and uninstalled it a number of times using "pip" but still the file is not there.
Check your pyarabic folder. Usually it's in Python27\Lib\site-packages\pyarabic.
There, there should be stack.py. If it doesn't exists, re-download pyarabic and then reinstall it.
After installation of pyarabic import STACK in this manner:
from pyarabic.stack import Stack
for window users
open cmd prompt and type the following to install the stack variable to python 3.x-
pip install pyarabic
To install and run with this code-
from pyarabic.stack import Stack
It seems like stack is not part of the Python Package Index so most probably it is a script you installed manually. The problem can be that the folder containing stack.py is not on your PYTHONPATH.
Open a terminal (Ctrl+ Alt + t) and edit the .bashrc file:
sudo gedit ~/.bashrc
Add the following line:
export PYTHONPATH=$PYTHONPATH:/path/to/the/folder/of/your/module
where you should substitute the part after the : to the full path to the directory
where stack.py can be found.
I hope this helps.

Categories