Profiling module in PyCharm - python

Following up on this question:
Create a PyCharm configuration that runs a module a la "python -m foo"
Running and debugging modules works great for me with the new option from 2018.1, for which you can now specify a module to run. However, profiling the same configuration does not work.
PyCharm tries to run something like this:
/path/to/python /path/to/pycharm/plugins/python/helpers/profiler/run_profiler.py 127.0.0.1 44725 -m mypackage.cli.runthis paramA paramB
For whatever reason, all parameters (especially the -m) are forwarded to my package, and argparse complains that it doesn't know -m. As I said before, this configuration works perfectly fine for running/debugging. Any idea how I could get the profiler working?

Related

VS Code Pytest Discovery Error Due to ModuleNotFoundError

I am trying to setup my VS Code debugger for pytest and am getting the error discovering pytests tests from the testing tab. The test I'm running works perfectly when run from the terminal but the VS debugger is not able to run it. In the testing tab, it directs me to the output where it shows a ModuleNotFoundError for logdna in the common.py file. That file has some other seemingly syntax errors but I believe VSCode is just not interpreting them correctly because the modules are in the code base. On the left you can see I ran pip3 install logdna to ensure logdna is actually installed.
Note that I am running inside a Poetry environment and have a .env in the project root directory.
Note also that I have reviewed and tried all answers provided here and none of the provided answers helped in my case: VSCode pytest test discovery fails
You are using virtualenv inside terminal. Here is clearly seeing in logs that python is installed inside /Users/.../pypoetry/virtualenvs/... folder.
But VSCode using default python (/usr/local/bin/python3) or at least non virtualenv version
You have to choose same interpreter in VSCode as terminal's one
What is virtualenv?

How to make the equivallent of .bat for mac and how to install python libraries on mac

I have this file.py:
import os
os.system("pip install pip")
os.system("pip install selenium")
How do I make it work for MAC and what is te equivallent of a .bat file in MAC to execute the file.py.
Your file.py script will generally work fine on Mac as long as the environment the script is running in is set up right. Most notably, the pip executable has to be findable via the current PATH variable. You might benefit by looking at the subprocess module, which is an alternative API for running external commands. It is a more robust mechanism for doing so.
The equivalent of a .BAT file is a shell script. You have a choice as to which shell to use to run the script. I think the most common source is the Bash shell. It is often the case that you use whatever shell is running at your command prompt. This functionality is generally much more general and flexible than a .BAT file is on Window. See this link for a discussion of many of the issues:
https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/shell_scripts/shell_scripts.html
A shell script can just be one or more commands that you might run in your Terminal. For example, to run test.py at a Terminal prompt, you'd do this:
> python test.py
The simplest equivalent in a shell script would be the same thing:
python test.py
A script that looks like this is run by whatever shell executes the shell script. What is more usually done is that a "shebang" line is added to the top of the shell script to explicitly define which shell will be used to run the script. So what the single line script above should really look like is this:
#!/bin/sh
python test.py
This may be starting to make your head spin. I would suggest reviewing the link I gave above, and possibly reviewing some other materials that explain shell scripts. Note that nothing about shell scripts is unique to the Mac. The concept is exactly the same on Linux, Unix, etc.
BTW, do you really want pip install pip? What does that do? Doesn't the pip package have to already be installed if the pip command is working?

Script working in ipython but not from the command line

I have a script that functions from within ipython but when I try and run the same script from the command line I receive import errors for a local module that I am trying to import:
from helper_functions.email_from_server import send_email
Error:
ImportError: No module named helper_functions.email_from_server
This script imports from within Ipython without any issues.
Comparatively, I have code that runs without any issues within ipython I can run another script using the command:
run script.py
From the command line I can run the same script:
python /dir/script.py
However this python /dir/script.py doesn't work with the script with local imports (from above) and I can't figure out if its a pythonpath issue or some local env issue? I have been reading through stack to find it but haven't been able to thus far. It feels like its just around the corner
One attempted solution:
PYTHONPATH=/dir/ python /dir/script.py
EDIT (to help clarify):
I am using an anaconda distribution on a linux machine.
Mucking about with PYTHONPATH is a recipe for sadness. You can do it, but you shouldn't. The correct thing to do is install your package in your correct environment. If you don't know how to create a package here's a super simple example. There may be some differences in your path when running via ipython vs command line.
You can find out what the differences are by using sys.executable and sys.path:
import sys
print(sys.executable)
print(sys.path)
Run that from IPython, and then run that from the python on your command line. You will undoubtedly get two different results. Since you're running Anaconda, you want to follow their guide for installing non-conda packages to install the one that you build.
Though of course that assumes that you've got the anaconda python on your path - you can check that out with which python since you're on Linux.
I resolved it via creating a wrapper shell script. Ugly in that i'm exporting the python path each time, but it works.
#!/bin/bash
export PYTHONPATH="${PYTHONPATH}:/my/dir"
source ~/.bash_profile
cd /my/dir && my/anaconda/location/bin/python /my/dir/to/script/cript.py

Python script: problems with shebang line (unix)

I am trying to get a feel for the Flask microframework by launching a test application to local server. When trying to run my code, app.py, I keep getting the error message:
-bash: ./app.py: /flask/bin/python: bad interpreter: No such file or directory
Here is the basic code (taken from here) for app.py, which lives in my todo-api directory:
#!/flask/bin/python
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
I've checked the file path to the python interpreter, and it should exist:
:bin $ pwd python
Users/me/Documents/Python/todo-api/flask/bin
I have followed the tutorial to the T; I've tried changing the shebang line to:
#!/flask/bin/python2.x
#!flask/bin/python
#!/flask/bin/env python
But to no avail. I am not that knowledgeable about bash, and have tried looking up what is going on, but the solutions to folks with similar problems have not worked for me; is there something going on behind the scenes that I am not understanding?
Bash shebangs expect an absolute path to the interpreter. So in your case you need to specify the full path to your Python interpreter i.e.:
#!/Users/me/Documents/Python/todo-api/flask/bin
You might want to investigate the use of /usr/bin/env python to be able to use the interpreter that is available in your user's $PATH environment variable. See https://unix.stackexchange.com/questions/12736/how-does-usr-bin-env-know-which-program-to-use/12751#12751
pwd tells you the current directory. It doesn't tell you where a command is located. The output from that command is a red herring.
You may be looking for which python. Put that path into your shebang line. Note that this will give you the Python interpreter from your $PATH, which may or may not be the right one.
The standard shebang line for Python scripts is
#!/usr/bin/env python
or
#!/usr/bin/python
I was having a similar issue with trying to setup a python script as an executable for testing some things and realized that bash was getting in the way more than it was helping. I ended up setting up pyinstaller (which is incredibly easy) and then making my script a stand alone executable without bash being in the mix.
Here's what I did (only takes a couple of minutes and no config):
First; pyinstaller needs: build-essential & python-dev
apt-get install build-essential python-dev
(or yum, etc... depending on your OS)
Then use the built in python package manager to setup pyinstaller:
pip install pyinstaller
That's it. Run: pyinstaller --onefile myapp.py (or pyinstaller.exe if your OS needs exe)
If it's successful (and it usually is), your new executable will be in a folder "Dist" in the same area you ran pysinstaller.

Package not importing when invoking Python script remotely

We have a python 2.7 script that executes LOCALLY perfectly fine.
The problem occurs ONLY when I attempt to invoke the script remotely (SSH):
ssh user#server "python script.py"
*Traceback (most recent call last):
File "script.py", line 18, in <module>
import requests
ImportError: No module named requests*
After doing a little bit of research, I have tried manually inserting the absolute the path to the "requests" library using sys.path.insert before importing the library:
sys.path.insert(1,'/usr/local/lib/python2.7/site-packages/requests-2.10.0-py2.7.egg')
import requests
I even confirmed the path to the package got added succesfully by printing the sys.path when invoking the script remotely:
print '\n'.join(sys.path)
Result:
/usr/local/lib/python2.7/site-packages/setuptools-20.9.0-py2.7.egg
/usr/local/lib/python2.7/site-packages/requests-2.10.0-py2.7.egg
/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages
However, I am still getting the "ImportError: No module named requests" no matter what.
Can you help me understand what am I missing?
Again, this script does find the library without issue and runs perfectly fine when I execute the script locally.
Thanks!
requests is a non-standard Python library. Make sure it is installed on the remote box you are attempting to execute the script on.
On the remote, execute from the shell:
easy_install pip
pip install requests
If I understand correctly, you are trying to run the same script on the same machine, but once from a local shell and once from a remote shell (with ssh).
If this is the case, when you use the remote shell, be sure that you have the environment properly setup.
This may boil down to just setting the PYTHONPATH environment variable correctly.
In your case, this would be:
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/:$PYTHONPATH
You may need to add more paths to it.
When I have this sort of issues, I usually run a python shell from the working environment and check where the files are taken from, so I can be sure the correct entries are set in the PYTHONPATH:
$ ipython
In [1]: import requests
In [2]: print requests.__file__
/usr/local/lib/python2.7/site-packages/requests/__init__.pyc
For this example, I would add /usr/local/lib/python2.7/site-packages/ to my PYTHONPATH if I was not able to import requests correctly.

Categories