Using windows 7 and python 2.7. I have a package named Regetron in c:\Python27\Lib\site-packages\regetron which contains __init__.py and engine.py. When I try to run this library from the command prompt by typing regetron I get the following error:
Traceback (most recent call last):
File "C:\Python27\Scripts\regetron.py", line 6, in <module>
from regetron.engine import Regetron
File "C:\Python27\Scripts\regetron.py", line 6, in <module>
from regetron.engine import Regetron
ImportError: No module named engine
I added c:\Python27\Lib\site-packages\regetron to %PYTHONPATH% and can successfully import this module from other scripts located in other folders as well as the interactive prompt, but for some reason it refuses to run from the command prompt. What is going on?
You actually have two problems here. Fixing either one of them would actually eliminate your immediate error, but you need to fix both of them.
When I try to run this library from the command prompt by typing regetron
You shouldn't have a script named regetron and also have a module or package named regetron. Fix it by renaming your script. But if you want to understand why it's causing a problem:
The current working directory is always part of sys.path. So, you're in the directory with regetron.py in, and you run it with regetron. That means that regetron.py is on the path. So when you import regetron, it finds your script, not the package. Or, when you from regetron import engine, it finds your script, and tries to import a variable/function/class/whatever named engine from it, rather than finding the package and trying to import a module underneath it.
I added c:\Python27\Lib\site-packages\regetron to %PYTHONPATH%
Never add a package's directory to sys.path.
Since site-packages is already on your sys.path, the code in regetron/engine.py is already available as regetron.engine. You don't want it to also be available as engine. This will cause all kinds of problems.
So, rename you script to something else, remove regetron from %PYTHONPATH%, and everything will be fine.
But you may want to (re-)read the section on Packages in the tutorial.
Related
I'm trying to run a python script containing submodules from Github to finetune its model.
The final step of the process is to invoke
python realesrgan/train.py -opt options/finetune_realesrgan_x4plus_pairdata.yml --auto_resume
However, doing from root of repo (and from realesrgan folder for that matter) so results in:
Traceback (most recent call last):
File "D:\Real-ESRGAN\realesrgan\train.py", line 5, in
import realesrgan.archs
ModuleNotFoundError: No module named 'realesrgan'
Yet, by launching python interactively from repo root (just "python") and then invoking
import realesrgan.archs
results in me being able to import it without any problem. All mentioned folders have required init.py in them.
This is my first time seeing discrepancy between calling a script and interactive python and I'm wondering, what can cause this.
System specifics just in case:
Windows 11
Python 3.9.9
python script execution fails from either powershell/cmd/file explorer
I'm trying to make a simple import and use the emailage third party library.
As per their documentation, the way to use their library is as follows:
pip install emailage-official
Then, simply import with:
from emailage.client import EmailageClient
The install works fine with pip - no errors. I double checked to see that the emailage package exists within the proper directory, and it does.
Package exists at:
C:\Users\aaron\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\emailage
This folder has (seemingly) the correct files with an __init__.py and everything. However, both pylint and command line interpreter throw me a
'No module named 'emailage.client'; 'emailage' is not a package' error.
The output of my sys.path is:
[...
'C:\\Users\\aaron\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages'
...
]
So the directory where emailage is installed is a part of the path... and lastly I pip-installed numpy just to test if it worked properly. Numpy installed to the same site-packages folder as emailage, and it works fine when it is imported, so I'm stuck.
I don't typically use Python much, so any and all help would be appreciated.
The issue was in the naming of my file.
I hastily named my file emailage.py and then tried to import from emailage.client.
I'm assuming that Python looked in my current directory and matched the names of the file I was working on before checking the installed third party libraries.
After renaming my file everything seems ok.
For others who run into similar problems -- beware of conflicting naming. Sometimes the simplest things trip you up the longest.
I ran into something similar and the answer from OP about namespace collision is what finally clued me in.
I was using the same name for both a sub-package (directory) and a module (file) within it.
For example I had this:
/opt/mylib/myapi
/opt/mylib/myapi/__init__.py
/opt/mylib/myapi/myapi_creds.py # gitignored file for user/pass
/opt/mylib/myapi/myapi.py # base module, load creds and connect
/opt/mylib/myapi/myapi_dostuff.py # call myapi.py and do work
The script 'myapi.py' imports credentials from myapi_creds.py via this statement:
from myapi.myapi_creds import my_user, my_pass
Testing the module 'myapi.py' resulted in this error:
$ ./myapi.py
Traceback (most recent call last):
File "./myapi.py", line 12, in <module>
from myapi.myapi_creds import my_user, my_pass
File "/opt/mylib/myapi/myapi.py", line 12, in <module>
from myapi.myapi_creds import my_user, my_pass
ModuleNotFoundError: No module named 'myapi.myapi_creds'; 'myapi' is not a package
The solution was to rename myapi.py to myapi_base.py so it's name does not collide with the sub-package name.
I took a look at this problem, and even though it is not exactly the same error that I encountered, it helped me solve it. I'll explain the situation I had, since I think some users might find this handy.
So, I was getting the following error log:
Traceback (most recent call last):
File "/home/kemal/Programming/Python/Preference_Articulation/LocalSearch/LS_apriori.py", line 1, in <module>
from LocalSearch.LocalSearch import LocalSearch
ModuleNotFoundError: No module named 'LocalSearch.LocalSearch'; 'LocalSearch' is not a package
The structure of my project is the following (using PyCharm):
View of project structure
The important thing to notice is that I separated my code into several folders, since it makes it more readable. Now, in the folder named LocalSearch I have 4 files, LocalSearch, LS_apriori and some 2 tests files (not relevant). When trying to run the file LS_apriori (which uses methods and classes from file LocalSearch) I was getting the error provided above. The code specifically is not important, and the way I handled the imports was the following:
from LocalSearch.LocalSearch import LocalSearch
The fix was simple. I renamed the py-file LocalSearch to Local_Search (just added an underscore). Afterwards, the error was gone.
So my problem was possessing a folder(package) with the same name as a file(module) inside it, which has a class inside it with the same name. Python didn't like that.
Having modules with the same name as packages inside them is fine however, I guess the class just added extra confusion.
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.
I'm not sure if the title of this question is correct, mods please feel free to change it.
I'm working through cs231n Convolutional Neural Networks for Visual Recognition course online and I've hit a weird error. This is my first attempt at any real Python programming so it could be a simple error, or something more complicated.
I'm using Python 2.7, Anaconda, and Windows 7.
When trying to run a script I get the following error;
NameError: global name 'im2col_cython' is not defined
My understanding so far is that this occurs because this fails (and calls the exception);
try:
from cs231n.im2col_cython import col2im_cython, im2col_cython
from cs231n.im2col_cython import col2im_6d_cython
except ImportError:
print 'run the following from the cs231n directory and try again:'
print 'python setup.py build_ext --inplace'
print 'You may also need to restart your iPython kernel'
I've tried to figure out why this may be the case. First off I have to run setup.py to turn im2col_cython.pyx into other files. This seems to run but does at one point have the warning;
warning: extension name 'im2col_cython' does not match fully qualified name 'cs231n.im2col_cython' of 'im2col_cython.pyx'
My figuring here is that it's an issue to do with the fact that the folder tree I have looks like this;
Assignment 2
->cs231n
Inside cs231n is where the setup.py and im2col_cython.pyx files are located. I've installed cython, and I have vc for python2.7 installed. When I run setup.py is creates 2 new files;
im2col_cython.c
im2col_cython.pyd
But I have no idea if that's what it's meant to do or anything. I'm in way over my head with this (0 Python knowledge), but I'm keen to learn!
EDIT 1
The script that imports im2col_cython is here
The im2col_cython file is here
EDIT 2
When including the following in the exception clause;
import traceback; traceback.print_exc()
I get this output;
Traceback (most recent call last):
File "fast_layers.py", line 3, in <module>
from cs231n.im2col_cython import col2im_cython, im2col_cython
ImportError: No module named cs231n.im2col_cython
Add this in the file fast_layer.py before import cs231n.im2col_cython:
import pyximport
pyximport.install()
The problem you mentioned should be fixed.
I am setting PYTHONPATH to have a directory that includes a few .py files.
When I go into python and type "import file", the file cannot be find (it says "No module named wsj10").
If, however, I cd to the directory, and repeat the same process, then the file is found.
I am just not sure why PYTHONPATH is being ignored. I followed exact instructions from installation instructions of some software, so I know I am doing the right thing.
Any circumstances under which PYTHONPATH will be ignored, or import won't work?
Thanks.
Following a comment below, here is a transcript:
untar file1.tgz to file1/. file1.tgz contains a library/file called file1.py.
type in the shell:
export PYTHONPATH=`pwd`/file1/:./
echo $PYTHONPATH shows the variable was set.
run python and type "import file1"
I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named file1
If I do first "cd file1" and then to import file1 it identifies the file.
Any circumstances under which PYTHONPATH will be ignored, or import won't work?
Yes. I've set PYTHONPATH in my /home/me/.bashrc and all worked ok from terminal, but when Apache w/ mod_wsgi starts my python scripts, it acts under sysem or dedicated user, which knows nothing of my .bashrc.
For this particular situation, I just used apache config to set python path for apache (WSGIPythonPath option).