Py.test collection phase taking very long - python

I am really quite new to development in Python in general, let alone testing with pytest. My problem is that the pytest collection phase runs unusually slow. I am specifying the test directory which contains only a handful of files with only one file containing three tests. The collection takes pretty much a whole minute, after which the actual tests run in under a few seconds. I have looked at similar questions but couldn't find a solution. I don't think it matters (as py.test is slow even from the command line) but I am using the pycharm IDE. The OS is Ubuntu.
This may be relevant: If I terminate the process after a few seconds I usually end up with a stacktrace ending as follows:
<A FEW LINES OMITTED...>
File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 413, in __call__
return self._docall(methods, kwargs)
File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 424, in _docall
res = mc.execute()
File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 315, in execute
res = method(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/_pytest/helpconfig.py", line 27, in pytest_cmdline_parse
config = __multicall__.execute()
File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 315, in execute
res = method(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 636, in pytest_cmdline_parse
self.parse(args)
File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 747, in parse
self._preparse(args)
File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 709, in _preparse
self._initini(args)
File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 704, in _initini
self.inicfg = getcfg(args, ["pytest.ini", "tox.ini", "setup.cfg"])
File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 861, in getcfg
if exists(p):
File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 848, in exists
return path.check()
File "/usr/local/lib/python2.7/dist-packages/py/_path/local.py", line 352, in check
return exists(self.strpath)
File "/usr/lib/python2.7/genericpath.py", line 18, in exists
os.stat(path)
KeyboardInterrupt
Or sometimes...
<STACK TRACE...>
File "/usr/local/lib/python2.7/dist-packages/py/_iniconfig.py", line 50, in __init__
f = open(self.path)
KeyboardInterrupt
Maybe one of the two last calls before the KeyboardInterrupt is very slow?
Please do ask for more detail should you require it!
Cheers!

Add PYTHONDONTWRITEBYTECODE=1 to your environment variables!
Windows Batch: set PYTHONDONTWRITEBYTECODE=1
Unix: export PYTHONDONTWRITEBYTECODE=1
subprocess.run: Add keyword env={'PYTHONDONTWRITEBYTECODE': '1'}
Note that the first two options are only valid for your current terminal session.
Here is how I found this out: pytest was being unusably slow from the command line, but working fine from within PyCharm. Copying the PyCharm command into cmd.exe (executes a small helper script) also was unusuably slow. Thus I printed out the environ variables at os.environ and tried it with that -- and it was fast! Then I eliminated each one-by-one.

I had the same problem. My fix was to set the Working directory setting in the Run/Debug Configuration to the folder where manage.py is located.

My solution was based off of this answer, where I did pytest dir/to/tests and it skipped the collection step entirely.

Related

Python script not running in Windows Task Scheduler

I have a simple python script that when I run it via command line it'll run. I also created a batch file to run the python script as well. When I double click that it'll run just fine. I can't get either the python script or the batch file containing the python script to run in Windows Task Scheduler and get and error see below. I just want to be able to run the Python script or batch file via task scheduler. I've tried multiple things from other qustions on here about putting the full path in the arguments and start in locations, and still get the same results.
This is my batch file code that works when double clicking the file just fine but won't run in task scheduler: "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\python.exe" "C:\Users\brant.evans\SQ Back Office-COR - General\PCMS_Part_Comments\Current_CSV_Output\PCMS_Parts_CSV_to_Excel.py" pause
Here is the error I get when running it via task scheduler:
Traceback (most recent call last):
File "C:\Users\brant.evans\SQ Back Office-COR - General\PCMS_Part_Comments\Current_CSV_Output\PCMS_Parts_CSV_to_Excel.py", line 7, in <module>
df_new = pd.read_csv('Current_PCMS_Parts.csv')
File "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 605, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 457, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 814, in __init__
self._engine = self._make_engine(self.engine)
File "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 1045, in _make_engine
return mapping[engine](self.f, **self.options) # type: ignore[call-arg]
File "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 1862, in __init__
self._open_handles(src, kwds)
File "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 1357, in _open_handles
self.handles = get_handle(
File "C:\Users\brant.evans\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 639, in get_handle
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'Current_PCMS_Parts.csv'
I just want to be able to either use the python script directly or the batch file that has the python script in it via Windows Task Scheduler so I can schedule it to run. I know that both work if I manually run them in the command prompt or just double clicking the batch file.
I actually found a solution that solved this issue. Inside of the batch file I put the following:
In the batch file insert after first line being usually #echo off the lines:
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0"
The batch file should additionally contain as last two line executed before exiting batch file processing the two lines:
popd
endlocal

How can I compile Bootstrap 4 scss with python webassets?

I'm trying to simply compile Bootstrap 4 with python webassets and having zero success. For now I'm just trying to do this within the bootstrap/scss directory so path issues are less of a big deal. Within this directory I have added a main.scss file with one line:
#import "bootstrap.scss";
I have a script called test_scss.py that looks like this:
from webassets import Bundle, Environment
my_env = Environment(directory='.', url='/')
css = Bundle('main.scss', filters='scss', output='all.css')
my_env.register('css_all', css)
print(my_env['css_all'].urls())
When I run this command, I get an error trace like this:
Traceback (most recent call last):
File "./test_scss.py", line 11, in <module>
print(my_env['css_all'].urls())
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/bundle.py", line 806, in urls
urls.extend(bundle._urls(new_ctx, extra_filters, *args, **kwargs))
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/bundle.py", line 765, in _urls
*args, **kwargs)
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/bundle.py", line 619, in _build
force, disable_cache=disable_cache, extra_filters=extra_filters)
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/bundle.py", line 543, in _merge_and_apply
kwargs=item_data)
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/merge.py", line 276, in apply
return self._wrap_cache(key, func)
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/merge.py", line 218, in _wrap_cache
content = func().getvalue()
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/merge.py", line 251, in func
getattr(filter, type)(data, out, **kwargs_final)
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/filter/sass.py", line 196, in input
self._apply_sass(_in, out, os.path.dirname(source_path))
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/filter/sass.py", line 190, in _apply_sass
return self.subprocess(args, out, _in, cwd=child_cwd)
File "/Users/benlindsay/miniconda/lib/python3.6/site-packages/webassets/filter/__init__.py", line 527, in subprocess
proc.returncode, stdout, stderr))
webassets.exceptions.FilterError: scss: subprocess returned a non-success result code: 65, stdout=b'',
stderr=b'DEPRECATION WARNING: Importing from the current working directory will
not be automatic in future versions of Sass. To avoid future errors, you can add it
to your environment explicitly by setting `SASS_PATH=.`, by using the -I command
line option, or by changing your Sass configuration options.
Error: Invalid CSS after "...lor}: #{$value}": expected "{", was ";"
on line 4 of /Users/benlindsay/scratch/python/webassets/test-2/bootstrap/scss/_root.scss
from line 11 of /Users/benlindsay/scratch/python/webassets/test-2/bootstrap/scss/bootstrap.scss
from line 1 of standard input
Use --trace for backtrace.
If I follow the instructions and set environment variable SASS_PATH=., that gets rid of that part of the error message, but I still get the error
Error: Invalid CSS after "...lor}: #{$value}": expected "{", was ";"
on line 4 of /Users/benlindsay/scratch/python/webassets/test-2/bootstrap/scss/_root.scss
from line 11 of /Users/benlindsay/scratch/python/webassets/test-2/bootstrap/scss/bootstrap.scss
from line 1 of standard input
Use --trace for backtrace.
I don't know SCSS syntax well yet, but I'd bet a lot of money this is me doing something wrong and not an error in the Bootstrap SCSS. Any thoughts of what I'm doing wrong would be greatly appreciated.
Turns out it actually kind of was a problem on Bootstrap's end. See https://github.com/sass/sass/issues/2383, specifically the quote:
This is a bug in our implementation—the parser shouldn't crash—but those Bootstrap styles aren't valid for Sass 3.5 as written.
Anyway, I just needed to update to the latest version of Ruby Sass (which apparently the webassets module depends on) and that fixed it.

Launch logme.py on boot

I have been trying to make a program launch on starting my system.
here is my problem..
ulogme (https://github.com/karpathy/ulogme) is a personal time tracker.
But after installation it needs to launched every time by running
$ python ulogme_serve.py
I want this script to run automatically on startup.
Solution by Atto Allas works well.
I am facing a new problem now,
I keep getting the following error
*
* Traceback (most recent call last):
File "/usr/lib/python3.5/site.py", line 580, in <module>
main()
File "/usr/lib/python3.5/site.py", line 566, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python3.5/site.py", line 287, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python3.5/site.py", line 263, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python3.5/site.py", line 253, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python3.5/sysconfig.py", line 595, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python3.5/sysconfig.py", line 538, in get_config_vars
_init_posix(_CONFIG_VARS)
File "/usr/lib/python3.5/sysconfig.py", line 410, in _init_posix
from _sysconfigdata import build_time_vars
File "/usr/lib/python3.5/_sysconfigdata.py", line 6, in <module>
from _sysconfigdata_m import *
ImportError: No module named '_sysconfigdata_m'**
Any suggestion and help are welcome
Thanks in Advance
If you want it to run at login
https://askubuntu.com/questions/48321/how-do-i-start-applications-automatically-on-login
Step 1: In the dashboard, search for 'startup applications'
Step 2: In the preferences screen that comes up, click 'add'
Step 3: Choose a name (for example, 'Ulogme Tracker'), a command to run (python ulogme_serve.py) and a comment ('Personal time tracker')
Step 4: Finally, click 'add'.
This should make it so that any time you log in, it will start ulogme_serve.py.
If you want it to run at system startup
https://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/
Step 1: Create a shell script called ulogme_startup.sh by typing sudo nano /etc/init.d/ulogme_startup.sh in terminal
Step 2: In the editor, type python ulogme_serve.py
Step 3: Hit 'ctrl+x' then 'y' and then hit 'enter'
Step 4: Back at the terminal, type sudo update-rc.d ulogme_startup.sh defaults
Step 5: Finally type sudo chmod +x ulogme_startup.sh
This should now run at system startup.
I hope this helps!
N.B. This will only work if ulogme_serve.py is on your path, if it isn't, change all instances of ulogme_serve.py to its absolute filepath. For example, if it was in a folder called 'ulogme' in your home folder, you would replace ulogme_serve.py with /home/<your username>/ulogme/ulogme_serve.py.

Django wsgi.py change monitor detecting changes when none have been made

I'm developing django based site on a windows machine, but I git push my changes to a headless Ubuntu server for staging purposes. When I git pull the changes into the working directory on the staging server I then need to run manage.py collectstatic. However, when I do this the change monitor ('Monitoring Code Changes' section of Reloading Source Code) reports that it has detected changed files:
monitor (pid=26216): Starting change monitor.
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: monitor (pid=26216): Change detected to 'myproject/manage.py'.
monitor (pid=26216): Triggering process restart.
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/envs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/envs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/envs/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 222, in run_from_argv
self.execute(*args, **options.__dict__)
File "/envs/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
File "/envs/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 385, in handle
return self.handle_noargs(**options)
File "/envs/myproject/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 160, in handle_noargs
% (destination_display, clear_display))
KeyboardInterrupt
I ran 'touch myproject/wsgi.py' to 'update the change cache' and then ran the collectstatic command again. This resulted in the same error.
I then entered the python console and ran the monitor starting command manually:
import myproject.monitor
myproject.monitor.start(1)
This made no difference. I also tried adding a conditional to the monitor so that it would ignore manage.py when checking for file changes but this just gave me the same error with myproject/__init__.py being the file it reported as having changed. DOes anyone know what is causing this?
I've worked out what was causing this. The legacy file modification times are held in a dictionary as unix timestamps with decimal places (e.g. 1366283624.92) while the actual modification times are returned by os.stat(path).st_mtime as integers. All I had to do was edit the monitor script at line 54, changing:
if mtime != _times[path]:
to
if int(mtime) != int(_times[path]):
I just hope this doesn't lead to any further problems down the road

WebPy: AttributeError No template named index

Recently I have been trying to learn WebPy and when attempting to use a template in the tutorial (http://webpy.org/docs/0.3/tutorial) I come across this error when trying to access the page.
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/application.py", line 239, in process
return self.handle()
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/application.py", line 420, in _delegate
return handle_class(cls)
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/application.py", line 396, in handle_class
return tocall(*args)
File "/Users/clement/Desktop/#Minecraft/index2.py", line 14, in GET
return render.index(name)
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/template.py", line 1017, in __getattr__
t = self._template(name)
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/template.py", line 1014, in _template
return self._load_template(name)
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/template.py", line 1001, in _load_template
raise AttributeError, "No template named " + name
AttributeError: No template named index
I've looked at this Question on SOF but I couldn't get it to work in my situation. I've spent about 4 hours trying to figure this out and have attempted to rework the way I launch the service which is usually done by:
Macintosh-2:~ clement$ python /Users/clement/Desktop/\#Minecraft/index.py
Thanks!
I believe I have found the answer.
To Solve:
CD into the directory containing main.py (or in my case index.py)
Make sure your HTML files are in a directory in the directory you cd'd into called 'templates'
run via: python [full path to main.py]
Hopes this helps people with similar issues :)
(Note: Running OS X 10.8.1)

Categories