ImportError where path to module is in PYTHONPATH - python

I'm not sure why I am still getting an ImportError
Traceback:
Traceback (most recent call last):
File "./test_jabba.py", line 12, in <module>
from tests import testbench
ImportError: No module named tests
Where error occurs:
from tests import testbench
from utils import gopher, jsonstream
I have this in my .bashrc
export PYTHONPATH=$PYTHONPATH:/Users/bli1/Development/QE/TrinityTestFramework/poc
However, when I echo $PYTHONPATH nothing is returned
I added __init__.py within directories tests and utils as well but same error occurs

Are you sure your .bashrc is correctly sourced?
If you run in your session
echo $PYTHONPATH
is it correctly set?
If not, try to manually export the path and then try to understand why your .bashrc is not being sourced. Possible trivial causes:
You are using a shell different than bash, very trivial but might happen. Try to run echo $SHELL. Note that this might return /bin/bash even if you are not actually using bash. Please refer to this post to find out which shell you are using. You can also start a new bash session manually (just run /bin/bash) and double check if PYTHONPATH is exported correctly there.
You modified your .bashrc but you didn't start a new session.

You should import the module and not the folder. The from something import something command only works for objects in the module file. Python module import error or python module import error should help

Related

python relative import from same package fails

I am working on a project involving the popular aiortc/aioquic github package. Located at src/aioquic/buffer.py there is an import statement:
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
This line is meant to import a C/stub module _buffer.c / _buffer.pyi . When used from the root directory to run examples found in the examples directory this import line will work, but when trying to run in a Docker container from the same directory this import fails and when importing from the /src directory the import fails. I have tried reading several other questions on python relative imports but none seem to make sense to me.
To reproduce the issue:
open python in the src directory
from aioquic import about (works)
from aioquic import buffer (fail)
from aioquic import buffer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/aioquic/src/aioquic/buffer.py", line 1, in <module>
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
ModuleNotFoundError: No module named 'aioquic._buffer'
I don't understand why the examples http3_client and http3_server work but when trying to import it directly it fails. I thought I understood roughly how to use python imports but apparently not. What would cause the same line to work when running from one directoy but fail when running from another directory? Is it something magical with the PYTHONPATH? I'm hoping someone smarter than I am can explain why this is failing.
[EDIT and most importantly how to fix this so the import will work nomatter where the user runs python from. I have tried several different combinations but none seem to work.]
Thanks in advance.
I figured out what was wrong.
The stub/C module had to be built into a .pyd file before it could imported. This was done by
pip install -e .
on the parent directory
Is it something magical with the PYTHONPATH?
Yes.
Typically . dot (current working directory) will appear
in that exported env var,
so where you started matters.
Take care to ensure you're using the same env var setting,
and the same pwd,
during both the interactive and the Docker runs.

I can't import a module in Python/Windows

I'm having problem when I trying to import a custom module I have which is very simple but I'm always getting:
Traceback (most recent call last):
File "demo_module1.py", line 12, in <module>
import mymodule
ModuleNotFoundError: No module named 'mymodule'
I have tried to set environment variables:
set PYTHONHOME=C:\Software\python-3.7.4
set PYTHONPATH=%PYTHONPATH%;C:\pyproys\test
Everything is located here: 'C:\pyproys\test'
The only way it works is if I add it directly in the code "But I don't want to do it in every single script I have so don't want to maintain it in that way".
import sys
sys.path.append('C:\pyproys\\test')
print(sys.path)
Here is the script I'm trying to run:
demo_module1.py
import mymodule
mymodule.greeting("Jonathan")
'mymodule.py' is in the same folder as 'demo_module1.py'
I'm expecting the code to run fine by just executing:
python demo_module1.py
Can someone please point me out what I'm doing wrong?
Try to find the directory /lib/site-packages in your Python folder in C drive and paste your module in that folder and then restart the system. Hope it'll solve your issue.

Importing a python module to enable a script to be run from command line

I'm new to python and trying to test a script from this github repo (https://github.com/mgp25/psn-api).
The root directory has an example.py and I'm trying to run it with
$ python example.py
which gives this error:
Traceback (most recent call last):
File "example.py", line 1, in <module>
from src.Auth import Auth
ImportError: No module named src.Auth
How can I get this to run?
There is a folder in the root directory named src but because I'm new to python I don't know how to connect things so that the src.Auth module gets imported (or if that's even the right terminology)
Python 3.3+ will happily interpret it as a package without an __init__.py, fwiw, and I believe that's what the author wrote in.
Also note from trying to run it just now, you'll need to install simplejson and requests. (Normally there'd be a requirements.txt or similar saying this.)
Being in the repository root directory, you do:
touch src/__init__.py
This will create an empty file but it is necessary for the Python module search system. Then you should be able to run it without problems, unless there is some dependency on external libraries.

flask.cli.NoAppException: Application crashing

My Flask Application is crashing when I'm trying to access it.
This is similar error to this or this. However, my set-up seems correct.
flask.cli.NoAppException: The file/path provided (server) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
My environment variable correctly set.
export FLASK_APP=server.py
export FLASK_DEBUG=1
My server file is server.py, and I don't have any __init__.py in the directory.
I don't recall having change anything special in the code. Where does the bug could come from?
The issue was that some package were missing or corrupted.
I reinstalled everything with pip3 install -r requirements.txt --ignore-installed and now it works fine.
This error may be a sign that an import is not found.
To see which import is missing, try to run your server.py file with the python interpreter:
python yourapp.py
Example of output :
Traceback (most recent call last):
File "yourapp.py", line 4, in <module>
from flask_httpauth import HTTPBasicAuth
ImportError: No module named flask_httpauth
Source (and other leads)
For me, solution was fixing a misspelled function name "create_app" in my _ init _.py
from flask import Flask
def create_app(test_config=None):
app = Flask(__name__)
return app
Super late answer, but I just ran into this and the solution was to use a Remote WSL connection in VS Code. I thought I was in the correct folder, but VS Code was looking at a very similar folder in Windows. You can tell you're in the wrong folder by running "touch somefile.abc" in your linux terminal, from the project folder, but that file doesn't automatically appear in VS Code's folder structure.
https://code.visualstudio.com/docs/setup/linux
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl

from twisted.words.protocols import irc no module named words.protocols

I am trying to create a simple irc client with twisted library and start by importing related module:
from twisted.words.protocols import irc
This error is displayed:
no module named words.protocols
I don't quite understand this error as I can import tiwsted directly (it is in my PATH then) and the filesystem is like this:
/usr/local/lib/python2.7/dist-packages/twisted/words/protocols/irc.py
The words and protocols folders both contain a __init__.py file, so they should be interpreted as modules.
If I change directory for /usr/local/lib/python2.7/dist-packages/twisted/words/ and start a python interpreter, this command runs without error:
from protocols import irc
But my final script would have nothing to do in this directory :/
I don't quite understand why I'm having this error and how to solve it.
Many thanks for helping
EDIT
I have all the words folders into /usr/local/lib and not in classic /usr/lib, maybe python is to look into the second folder and not the first one.
I don't know yet how to overload this behavior, and hope it does not boils down to overriding os.environ['PATH'] which looks really dirty.
EDIT2: /usr/local/lib folders belong to the PATH as shown by this command:
python -c "import sys; print [ i for i in sys.path if 'local' in i ]"
['/usr/local/lib/python2.7/dist-packages']
So I still don't understand why this shit is not going to be imported
EDIT3
This is not script name related I guess; from a terminal in my debian :
python -c "from twisted.words.protocols import irc"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named words.protocols

Categories

Resources