I am trying to use the memory_profiler module to profile the memory usage of a large Python program. There seems to be a memory leak somewhere in my program, so I'm hoping this module will help me find the leak.
I installed memory_profiler using pip and tested it with the sample code provided here. This works perfectly.
When I try to use it with my program, I add the #profile decorator to my main() function and run the profiler from the command line the same way:
$ python -m memory_profiler engine.py
I get the following error, and my program fails to run (everything hangs):
usage: memory_profiler.py [-h] [-c CHARSET] [-i] [-o] [-l LOG_LEVEL] [-g] [-k]
[-w] [-s STOP] [-x TEXTS] [-z SIZE] [-t TIMEOUT]
[-p] [-d DEVICE]
memory_profiler.py: error: unrecognized arguments: engine.py
Any ideas what I might be doing wrong?
Related
I have a Python script that creates a virtual environment in the following way:
import os
import subprocess
virtualenv_path = os.path.join("/tmp", "my-environment")
subprocess.run(["virtualenv", virtualenv_path], check=True)
This works just fine. However in the Virtualenv documentation I saw that you can "trigger invocation of Python environments from within Python". To do this "you should be using the virtualenv.cli_run method; this takes an args argument where you can pass the options the same way you would from the command line. The run will return a session object containing data about the created virtual environment."
So I ran the following instead:
from virtualenv import cli_run
cli_run(["virtualenv", "/tmp/my-environment"])
However this gives me the following error:
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--read-only-app-data] [--app-data APP_DATA] [--reset-app-data] [--upgrade-embed-wheels] [--discovery {builtin}] [-p py] [--try-first-with py_exe]
[--creator {builtin,cpython3-posix,venv}] [--seeder {app-data,pip}] [--no-seed] [--activators comma_sep_list] [--clear] [--no-vcs-ignore] [--system-site-packages] [--symlinks | --copies] [--no-download | --download]
[--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip] [--no-setuptools] [--no-wheel] [--no-periodic-update] [--symlink-app-data] [--prompt prompt] [-h]
dest
virtualenv: error: unrecognized arguments: /tmp/my-environment
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
I am really unsure about why this doesn't work as I am passing the same arguments as I would on the command line.
When you call cli_run as part of virtualenv, you don't need to include the program name, in this case "virtualenv". Simply include your destination, along with any other arguments.
I am playing around with this Instagram ID tracker because my friends are constantly changing usernames so I can never keep up. The code is from here. The only way I am aware of, is that you have to go to any sort of terminal and type a command in this format
$ usage: t.py [-h] -u USERNAME [-p] [-s] [-t]
I've already tried to do some on my own and I recently saw other people running command lines in the Python text editor but it doesn't seem to work for me. This is what I did
import os
import main
import api
os.system('python3 main.py -u <USERNAME>')
There are 2 files, main.py and api.py. I imported both but when I run it, it still says
usage: t.py [-h] -u USERNAME [-p] [-s] [-t]
t.py: error: the following arguments are required: -u/--username
Is there an easier way to go about this?
My overall aim is to run the Deep_Learning_keras which is a SpaCy example in Google Colab (ie in Jupyter Notebooks)
The GitHub link to the Deep_Learning_keras SpaCy example is:https://github.com/explosion/spaCy/blob/master/examples/deep_learning_keras.py
When I ran the example code directly in Google Colab I got the following error:
usage: ipykernel_launcher.py [-h] [-r] [-H 64] [-L 100] [-d 0.5] [-e 0.001]
[-i 5] [-b 256] [-n -1][model_dir] [train_dir] [dev_dir]
ipykernel_launcher.py: error: unrecognized arguments: -f
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
I understand that the example was originally written for python and that running the copy in a Jupyter Notebook environment is not entirely compatible. I'm not sure what code/statements I need to change in the example to make it compatible
I did a google search for the SystemExit: 2 error and the closest example I found was (however the problem within the SystemExit: 2 is different to my problem):
SystemExit: 2 error when calling parse_args() in iPython Notebook
Thanks
I thought I'd post this here because hours of googling ended up fruitless. I have an obj file and I'm trying to use the python script included with three.js to export it for use in three.js however I have never used python before and don't know what I'm doing.
These have been my steps so far:
I've opened up the Python GUI, opened up convert_obj_three.py, clicked run -> run module, and then I get this:
======= RESTART: C:\three.js\utils\converters\obj\convert_obj_three.py =======
Usage: convert_obj_three.py -i filename.obj -o filename.js [-m morphfiles*.obj] [-c morphcolors*.obj] [-a center|top|bottom] [-s flat|smooth] [-t binary|ascii] [-d invert|normal]
When I try typing in something like convert_obj_three.py -i test.obj -o test.js I get an SyntaxError: invalid syntax error.
test.obj is in the same folder as convert_obj_three.py however that does not seem to be the issue as no matter what I type I seem to get a syntax error...
Run the command:
convert_obj_three.py -i test.obj -o test.js
at a command prompt. On Windows, Python will have to be in your path for this to work, that's an option in the installer (at least in 2.7) right at the bottom of the list of options, IME sometimes not visible - very easy to miss - so scroll down to find it.
In my python script myscript.py I use argparse to pass command-line arguments. When I want to display the help information about the input arguments, I just do:
$ python myscript.py --help
If instead I want to use ipython to run my script, the help message won't be displayed. Ipython will display its own help information:
$ ipython -- myscript.py -h
=========
IPython
=========
Tools for Interactive Computing in Python
=========================================
A Python shell with automatic history (input and output), dynamic object
introspection, easier configuration, command completion, access to the
system shell and more. IPython can also be embedded in running programs.
Usage
ipython [subcommand] [options] [files]
It's not so annoying, but is there a way around it?
You need to run your .py script inside the ipython. Something like that:
%run script.py -h
This is an IPython bug, corrected in https://github.com/ipython/ipython/pull/2663.
My 0.13 has this error; it is corrected in 0.13.2. The fix is in IPthyon/config/application.py Application.parse_command_line. This function looks for help and version flags (-h,-V) in sys.argv before passing things on to parse_known_args (hence the custom help formatting). In the corrected release, it checks sys.argv only up to the first --. Before it looked in the whole array.
earlier:
A fix for earlier releases is to define an alternate help flag in the script:
simple.py script:
import argparse, sys
print(sys.argv)
p = argparse.ArgumentParser(add_help=False) # turn off the regular -h
p.add_argument('-t')
p.add_argument('-a','--ayuda',action=argparse._HelpAction,help='alternate help')
print(p.parse_args())
Invoke with:
$ ./ipython3 -- simple.py -a
['/home/paul/mypy/argdev/simple.py', '-a']
usage: simple.py [-t T] [-a]
optional arguments:
-t T
-a, --ayuda alternate help
$ ./ipython3 -- simple.py -t test
['/home/paul/mypy/argdev/simple.py', '-t', 'test']
Namespace(t='test')