Python: Export all used modules - python

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.

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.

Can I debug a python package after installing it?

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.

(Dumb noob) Will changing directory in cmd.exe prior to installing a third party module, cause that module to be stored in that directory?

I want to install python, and then some additional modules, to my computer (windows).
I've been told that the best way to install python and packages is to make a folder in your drive somewhere BEFORE installing anything, (ex. 'Python') then pointing all your downloads (using cd in the command line) to this folder so that everything is in once place, and you don't have to go on a wild goose chase to make sure everything you want access to is in your PATH when you go to import modules etc.
Do I have the right idea?
I have had trouble importing modules in the past because they were not in the Path.
Will changing the directory in the command line before typing:
pip install somemodule
cause that module to be saved to where I just changed the directory to?
pip always installs the libraries in a fixed directory, usually in the user folder. You can check this by the command pip show <installed-package-name>
So you can use any package you already installed to get the pip directory. The location might vary based on your python version and env name.
Example: c:\users\<user>\appdata\roaming\python\python37\site-packages

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.

Python: using downloaded modules

I am new to Python and mostly used my own code. But so now I downloaded a package that I need for some problem I have.
Example structure:
root\
externals\
__init__.py
cowfactory\
__init__.py
cow.py
milk.py
kittens.py
Now the cowfactory's __init__.py does from cowfactory import cow. This gives an import error.
I could fix it and change the import statement to from externals.cowfactory import cow but something tells me that there is an easier way since it's not very practical.
An other fix could be to put the cowfactory package in the root of my project but that's not very tidy either.
I think I have to do something with the __init__.py file in the externals directory but I am not sure what.
Inside the cowfactory package, relative imports should be used such as from . import cow. The __init__.py file in externals is not necessary. Assuming that your project lies in root\ and cowfactory is the external package you downloaded, you can do it in two different ways:
Install the external module
External Python packages usually come with a file "setup.py" that allows you to install it. On Windows, it would be the command "setup.py bdist_wininst" and you get a EXE installer in the "dist" directory (if it builds correctly). Use that installer and the package will be installed in the Python installation directory. Afterwards, you can simply do an import cowfactory just like you would do import os.
If you have pip or easy_install installed: Many external packages can be installed with them (pip even allows easy uninstallation).
Use PYTHONPATH for development
If you want to keep all dependencies together in your project directory, then keep all external packages in the externals\ folder and add the folder to the PYTHONPATH. If you're using the command line, you can create a batch file containing something like
set PYTHONPATH=%PYTHONPATH%:externals
yourprogram.py
I'm actually doing something similar, but using PyDev+Eclipse. There, you can change the "Run configurations" to include the environment variable PYTHONPATH with the value "externals". After the environment variable is set, you can simply import cowfactory in your own modules. Note how that is better than from external import cowfactory because in the latter case, it wouldn't work anymore once you install your project (or you'd have to install all external dependencies as a package called "external" which is a bad idea).
Same solutions of course apply to Linux, as well, but with different commands.
generally, you would use easy_install our pip to install it for you in the appropriate directory. There is a site-packages directory on windows where you can put the package if you can't use easy_install for some reason. On ubuntu, it's /usr/lib/pythonX.Y/dist-packages. Google for your particular system. Or you can put it anywhere on your PYTHONPATH environment variable.
As a general rule, it's good to not put third party libs in your programs directory structure (although there are differing opinions on this vis a vis source control). This keeps your directory structure as minimalist as possible.
The easiest way is to use the enviroment variable $PYTHONPATH. You set it before running your scripts as follows:
export $PYTHONPATH=$PYTHONPATH:/root/externals/
You can add as many folders as you want (provided their separate by :) and python will look in all those folders when importing.

Categories