ModuleNotFoundError while running pytest from cmd - python

I'm running a pytest test with cmd (from a .bat file) and getting a ModuleNotFoundError.
But when running it from PyCharm, it works fine.
I looked around and saw some solutions regarding PYTHONPATH but that doesn't seem to work.
Any idea how to solve this?
Thanks

Have you tried running:
$ python -m pytest /test/...
Instead of $ pytest /test/... you can find more on it in pytest docs

Related

Pipenv script won't run on Windows using Git Bash

I am running Gitbash for windows and have installed pipenv using pip. Yet when I invoke pipenv nothing happens:
Since there isn't a command not found error, I believe the script is recognized it just isn't running properly.
Pipenv is installed globally:
I also can confirm that the Scripts folder is in the file path:
I don't think it is a problem with Gitbash because I can run other pip packages in the same scripts folder:
I'm also able to run python pipenv.exe but not pipenv.exe when in the Scripts folder:
I've struggled with this myself untill just now.
I've got a few suggestions.
SOLUTION 1:
I was calling Poetry to try and make sense of it as well and then I tried calling both applications from Powershell. Both work as inteded.
SOLUTION 2:
Calling python -m pipenv, as sugested in this other Stack Overflow thread, also works as intended. You could alias the command to pipenv in git bash and call it a day too.
Quoting the docs on the -m flag:
When called with -m module-name, the given module is located on the Python module path and executed as a script.
Quoting appdividend.com:
The -m flag in Python searches the sys.path for the named module and executes its contents as the __main__ module.
It goes through whatever path is in which pipenv and executes the module as a script.
SOLUTION 3:
This is how I ended up fixing my Pipenv blunder.
Reinstall Python and all packages to AppData.
MINGW64 was having trouble seeing Pipenv in C:\Program Files. I also noticed that I had some packages in AppData\Roaming as well, so I figured I'd reinstall Python, unticking the Install for all users option (to trigger AppData installation) to see if I could wrangle all packages together.
After that I tried installing Pipenv and succeeded in calling it as expected.
This is all highly anecdotal. I have no idea why MINGW64 failed to call JUST Pipenv and not Poetry, but this is how I've fixed it on my end.

Python script stops working when run outside of pycharm

I wrote my first program with a non built-in module and it works when I run it in pycharm, but when I try to run it in cmd I get an error: app.py, line 1, in <module> from pytube import YouTube ModuleNotFoundError: No module named 'pytube'
I've tried installing pytube module via cmd once again, but the problem still occurs
EDIT: The rest of my scripts are working fine (there are using built-in only modules)
Have you tried installing Pytube with pip on your command prompt? Enter: pip3 install pytube in your terminal and rerun the script. It wasn't clear with the original post if you already did this.
If this doesn't work, try running your script with the -m flag: $ python -m main
A more detailed answer for this issue is already available here.
Did you create an environment when you were in pyCharm? There's a chance you have to activate the env when you are in the cmd prompt.
Console without an environment
C:\path>
Activate environment named 'base'
C:\path>activate base
Console with an environment named 'base'
(base) C:\path>
Ok, so after some research and trying different solutions, I managed to run my script just by copying the missing module to the same directory as my app.py file, I don't know if it's the best solution, but it works.

pytest or py.test, and inconsistent documentation

I decided to give it a go.
Virtualenv is up (python 3.5.2) pip install -U pytest (according to: https://docs.pytest.org/en/latest/getting-started.html).
Next documentation says I should run:
$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py
But this is actually what I got:
$ pytest --version
Usage: pytest [OPTIONS] [testfile [testpattern]]
examples:
pytest path/to/mytests.py
pytest path/to/mytests.py TheseTests
pytest path/to/mytests.py TheseTests.test_thisone
pytest path/to/mytests.py -m '(not long and database) or regr'
pytest one (will run both test_thisone and test_thatone)
pytest path/to/mytests.py -s not (will skip test_notthisone)
pytest: error: no such option: --version
But if I run:
py.test --version
This is pytest version 3.9.1, imported from ${HOME}/${V_ENV}/lib/python3.5/site-packages/pytest.py
According to that answer:
"py.test" vs "pytest" command
py.test is old (deprecated they say) and pytest is way to go.
I checked both of them:
$ which pytest
$HOME/$V_ENV/bin/pytest
$ which py.test
$HOME/$V_ENV/bin/py.test
Exactly same files.
$HOME is my home, but $V_ENV is where I keep my virutalenvs (I use virtualenvwrapper).
When running tests:
It works:
$ py.test tests/
It does not (exception):
$ pytest tests/
I checked stack trace.
py.test runs using python3 (correct)
pytest runs using python2 (incorrect, it's from os)
Anyone knows what is going on?
Also looks like py.test and pytest are exactly the same, so this notion of not using py.test seems a bit obsolete. Am I correct in saying that?
As it happens py is installed in system python libraries (as probably yet another dependency of something). There is a consistent way to to reproduce the bug. It involves following steps.
create virtualenv
run pytest --version (it will complain that pytest has no --version)
pip install pytest
run pytest --version (it will complain that pytest has no --version)
And it will stay this way. However if you deactivate and activate env or
outright close shell and reopen it, it will fix it.
=== Old Anwer ===
Thanks to #hoefling, he nudged me correct direction!
There is an old library called py that is installed as dependency of tox. It looks like that under certain circumstances related to installation and/or running those two libraries you may end up with corrupted install. Initially I thought it was outright order of installation (install tox first then pytest second) but I was unable to reproduce that. So current working theory is that I run tox first causing it to create invalid virtualenv and then I have installed pytest that got corrupted PYTHONPATH or PATH.
In short. There is a way to corrupt with tox your pytest, if it happens start from scratch people!

pyInstaller: ImportError: No module named 'praw'

I wanted to pack my script using pyInstaller. I run pyinstaller file.py -F, file is created successfully, but when running I get ImportError: No module named 'praw'. So I created new file containing only import praw and run pyinstaller file.py -F --hidden-import=praw but still get the same error when running.
I was unable to find anything similar, most issues were solved by using --hidden-import.
Any ideas on how it can be solved?
EDIT:
praw is installed inside virtual environment and running the script directly works as expected.
Seem pyinstaller run outside the virtualenv.
Try switch to your virtualenv and run:
python -m PyInstaller -F file.py
I'll recommend to look at pyenv or virtualenv. Activate these env's and install the praw module here. This should work.
This command might help you out. It installs the Praw module for you. Make sure you have pip installed!
pip install praw
I found a way to solve the issue:
When using Python2.7, or starting the shell like python2, we need to do
python2 -m pip install --user praw
to make sure they are linked during installation.
Same idea for python3 shell.

Installed Nose but cannot use on command line

I installed Nose on a Mac OSX 10.10.5 with Python2.7.9 using easy_install. The installation appeared to be successful:
Collecting nose
Downloading nose-1.3.7-py2-none-any.whl (154kB)
100% |████████████████████████████████| 155kB 2.3MB/s
Installing collected packages: nose
Successfully installed nose-1.3.7
But now, when I try even basic stuff with nosetests on the command line, like nosetests -h or which nosetests I just get:
bash: nosetests: command not found
I have tried uninstalling, reinstalling using pip, tried installing with sudo and then running sudo nostests in the directories with tests scripts as other posts have suggested, but nothing seems to work.
The original purpose for installing was to use nose to run some basic tests with tests scripts I had written for these simple web.py apps. But nothing works, just keep getting the command not found response.
What's strange is that, when I open up the Python interpreter in Terminal, and do something like:
import nose
nose.main()
I get the expected result of:
.
----------------------------------------------------------------------
Ran 1 test in 0.135s
OK
So clearly it's installed....somewhere. Any suggestions for what the hell is going on here?
There are lots of error occurred when using pip install packages on Mac OS. So I recommend you install nose using easy_install.
$ pip uninstall nose
$ sudo easy_install nose
Then you can try nosetests now :)
I had this exact issue on OS X EI Captain with Python 2.7.10.
First I installed nose using pip:
$sudo pip install nose
which failed on the first attempt. Went through on the second attempt. But the nosetests command didn't work.
In order to fix this:
Step 1: Don't uninstall nose if it was installed already using pip as in my case.
Step 2:
$cd /usr/bin
$sudo easy_install nose
Above command finds the nosetests script (which was installed by pip earlier) & sets it under /usr/local/bin
Step 3: Try nosetests
$nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.047s
OK
On UNIX-like systems like OS X, the script should be in /usr/local/bin. Make sure that directory is in the PATH environment variable in the shell that you use.
If not, you can also locate it using find, e.g:
find / -type f -name 'nosetests*' -perm +111 -print -quit
This means; search for a file whose name starts with nosetests, which has execute permissions set. Print the path name and stop.
I found that going to
Library/usr/bin
and running
sudo easy_install nose
it seems that sometimes it doesn't automatically install nose (and therefore nosetests functionality). Do the above lines, and you should be a-ok.
I wish i had a better explanation for why this happened, but i'm still pretty new, myself.
I had to use
Nosetest
or
python3 -m "nose"
Apparently this is the way Nosetest should be used in Python3. See also
How to make nosetests use python3
First, can you run 'python' from the command line? nosetests should be in that same directory:
rich bin $ which python
/home/rich/anaconda/bin/python
rich bin $ which nosetests
/home/rich/anaconda/bin/nosetests
It should also be in the downloaded nose package:
rich bin $ find /home/rich/anaconda -name nosetests
/home/rich/anaconda/pkgs/nose-1.3.3-py27_0/bin/nosetests
/home/rich/anaconda/pkgs/nose-1.3.7-py27_0/bin/nosetests
/home/rich/anaconda/bin/nosetests
From what I understand, everyone is moving to pytest - an actively-maintained testing framework.
It's not a solution to this problem, but it's likely the most-appropriate choice if you are still using nose.
I try to reinstall the pip, it doesn't work but lastly, when i use sudo ...it works
pip3 uninstall nose
sudo pip3 install nose
and
which nosetests
/usr/local/bin/nosetests
This can also happen if you were running nose within a virtual environment, and that virtual environment has been deactivated. If this is the case, reactivate with source bin/activate.

Categories

Resources