Can I debug a python package after installing it? - python

I'm interested in a package and wanted to play around with the code: https://github.com/aiqm/torchani
The package itself is not complex and the key modules are included in the torchani folder. I wanted to use the VSCode debugger to do some experiments with the components and track the code. Do I need to run python setup.py --install, or I should simply go to the folder and run the modules without installing?
The problem is: there will be a lot of relative import issues if I directly run the code in the parent folder. If I install the package, then the code will probably be compiled and my changes will not be executed.

You can install the package with python setup.py install (or pip install [-e] .).
For the debugging part you can use the debugger of VSCode, just set justMyCode: False in the launch.json.

Related

Python with virtual evironment not working with my own package

My project looks like this:
src
|--annotation_correction
|--|--suggestion.py
|--|--error.py
|--|--...
venv
|--...
I installed my package, using pip install -e . while in the main folder.
When I type pip freeze, my package "annotation" is in the list and VSCode also seems to recognize it as an installed package.
The problem is that when I run suggestion.py while trying to import e.g. from error.py with from annotation_correction.error import Error, ErrorType, I still get the error:
ModuleNotFoundError: No module named 'annotation_correction.error'; 'annotation_correction' is not a package
All this while using the interpreter that is running in the venv.
My setup.py just calls setup() and my setup.cfg looks like this:
...
packages =
annotation_correction
...
package_dir =
=src
To make my comment an answer:
If you have a module in a package that imports other modules from the same package with from package import module or from . import module or similar, then you will need to use the -m switch:
Search sys.path for the named module and execute its contents as the __main__ module.
Otherwise, Python will have set up sys.path so the directory of the script that is run is in it, which will naturally wreak havoc with imports (since the package is "invisible").
This stands whether or not you've installed a package (and indeed you don't necessarily need to pip install -e the same package you're working on) or not.
The other option is to have a standalone script outside the package that acts as the entry point and does e.g. from annotation_correction.suggestion import main (since from that script's point of view, the package is there and all is fine -- and if the package is installed, -e or no -e, the script doesn't even need to be next to the package directory, since the package has been installed and as such on the search path), but that's unnecessary.
Visual Studio Code apparently supports the same thing with a "module" run configuration.

How to run python project on Windows

I'm a mobile dev, no experience with backend environment. I'd appreciate some steps to config my Windows to run python projects, like this one: https://github.com/avilash/TikTokAPI-Python
I can't figure out how to config, run, and test those methods that are described. I assume it should be an IDE for python, but not sure which one is good for Windows, and how ton config it to run a project like this.
On mobile it's a lot easier, just download project, import in the IDE and run it. Any best practices for experienced devs are appreciated!
install python on your pc, then do pip3 install -r requirement.txt . I personally prefer vscode. Its the best with required extension installed, or if you are the newbie and you want to learn python, then don't install extension.
Download the standard Python distro from here https://www.python.org/downloads/. I would stick with versions 3.6 or 3.7 for now for stability and compatibility reasons.
Run the installer, the default installation path will be something like C:\Program Files\Python3X\. Since you're installing it for the first time, it will be your only installation, so you can choose during the install to set your environment variables (typically including PATH and PYTHONPATH) to point to this installation as the default one.
Create your project folder anywhere you like, where you can put your .py scripts. You can then cd into the folder in the command line and run any scipt like this python myscript.py (or if you haven't set the default env vars C:\Program Files\Python3X\python.exe myscript.py).
This is the bare minimum you need to be able to run Python projects.
To install the package you mentioned, in the command line type pip install git+https://github.com/avilash/TikTokAPI-Python (or C:\Program Files\Python3X\Scripts\pip.exe install git+https://github.com/avilash/TikTokAPI-Python). This will work because the project has a setup script (setup.py). Note that this will install it into the Python installation folder and you will NOT be able to modify it. If you want to be able to do development on it, clone it into a separate folder, navigate into it and install in 'dev' mode from the current folder using pip install -e . (note the dot!)
For the IDE, you can use VS Code if you're already using it, just install the Python extension. If you're more into IntelliJ, you can then use PyCharm which will have the familiar interface but it's a separate IDE. Both are similarly good in terms of features and maturity.
Other notes you can find useful at the beginning:
If you install multiple Python installation in different places, they are pretty much self-contained and defined by their installation path, but of course the one for which you will select to set the environment vars during installation will be the default one. If you want to run the pip command for a specific installation, then you will have to use C:\Program Files\Python3X\python.exe -m pip install ....
pip is Python package manager, typically you can install anything available on https://pypi.org/ with just pip install pandas for example.
You can check all packages installed using pip list. Since pip itself is also just a Python package, it will always be in the list as it's installed by default when you install Python.

Python: Export all used modules

I'm using a lot of modules installed by Internet.
It's possible to write a script to copy automatically all of these module in a folder?
I don't know where these modules are, I only write:
import module1
import module2
I simply want that module1 and module2 can copied in a folder in order to use my file.py in other pc witouth installing any software except for Python.
pip and virtualenvs. You develop locally on a virtualenv, installing and uninstalling whatever you want. When your code is exportable make a list of requirements with command "pip freeze". Then you carry your code to another computer, without any other code except for the output of "pip freeze" in a file called "requirements.txt". Do a "pip install -r requirements.txt" and... ¡magic! All are installed in their proper path.
If you are interested in where those modules are, find your python path and find a "site-packages" folder (on Windows it usually is "C:\PythonX.X\lib\site-packages" or something like that). But I'm 100% sure you will regret copying modules manually from here to there.

PYTHONPATH vs symbolic link

Yesterday, I edited the bin/activate script of my virtualenv so that it sets the PYTHONPATH environment variable to include a development version of some external package. I had to do this because the setup.py of the package uses distutils and does not support the develop command à la setuptools. Setting PYTHONPATH works fine as far as using the Python interpreter in the terminal is concerned.
However, just now I opened the project settings in PyCharm and discovered that PyCharm is unaware of the external package in question - PyCharm lists neither the external package nor its path. Naturally, that's because PyCharm does not (and cannot reliably) parse or source the bin/activate script. I could manually add the path in the PyCharm project settings, but that means I have to repeat myself (once in bin/activate, and again in the PyCharm project settings). That's not DRY and that's bad.
Creating, in site-packages, a symlink that points to the external package is almost perfect. This way, at least the source editor of PyCharm can find the package and so does the Python interpreter in the terminal. However, somehow PyCharm still does not list the package in the project settings and I'm not sure if it's ok to leave it like that.
So how can I add the external package to my virtualenv/project in such a way that…
I don't have to repeat myself; and…
both the Python interpreter and PyCharm would be aware of it?
Even when a package is not using setuptools pip monkeypatches setup.py to force it to use setuptools.
Maybe you can remove that PYTHONPATH hack and pip install -e /path/to/package.
One option is to add path dynamically:
try:
import foo
except ImportError:
sys.path.insert(0. "/path/to/your/package/directory")
import foo
But it is not the best solution because it is very likely that that code will not get into the final version of the application. One more option (and more appropriate imho) is to make simple setup.py file for package and deploy it in virtualenv with develop parameter or by pip with -e parameter:
python setup.py develop
or:
pip install -e /path/to/your/package/directory
http://packages.python.org/distribute/setuptools.html#development-mode
This is an improvement on ndpu's answer that will work regardless of where the real file is.
You can dereference the symlink and then set sys.path before importing local imports.
import os.path
import sys
# Ensure this file is dereferenced if it is a symlink
if __name__ == '__main__' and os.path.islink(__file__):
try:
sys.path.remove(os.path.dirname(__file__))
except ValueError:
pass
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
# local imports go here

Developing Python Module

I'd like to start developing an existing Python module. It has a source folder and the setup.py script to build and install it. The build script just copies the source files since they're all python scripts.
Currently, I have put the source folder under version control and whenever I make a change I re-build and re-install. This seems a little slow, and it doesn't settle well with me to "commit" my changes to my python install each time I make a modification. How can I cause my import statement to redirect to my development directory?
Use a virtualenv and use python setup.py develop to link your module to the virtual Python environment. This will make your project's Python packages/modules show up on the sys.path without having to run install.
Example:
% virtualenv ~/virtenv
% . ~/virtenv/bin/activate
(virtenv)% cd ~/myproject
(virtenv)% python setup.py develop
Virtualenv was already mentioned.
And as your files are already under version control you could go one step further and use Pip to install your repo (or a specific branch or tag) into your working environment.
See the docs for Pip's editable option:
-e VCS+REPOS_URL[#REV]#egg=PACKAGE, --editable=VCS+REPOS_URL[#REV]#egg=PACKAGE
Install a package directly from a checkout. Source
will be checked out into src/PACKAGE (lower-case) and
installed in-place (using setup.py develop).
Now you can work on the files that pip automatically checked out for you and when you feel like it, you commit your stuff and push it back to the originating repository.
To get a good, general overview concerning Pip and Virtualenv see this post: http://www.saltycrane.com/blog/2009/05/notes-using-pip-and-virtualenv-django
Install the distrubute package then use the developer mode. Just use python setup.py develop --user and that will place path pointers in your user dir location to your workspace.
Change the PYTHONPATH to your source directory. A good idea is to work with an IDE like ECLIPSE that overrides the default PYTHONPATH.

Categories