I want to add a new class to PICOS, a python module. I installed it the normal way a long time ago. But now I have downloaded the source and I a am trying to make some changes.
The problem is that I cannot manage to ask python to load the module from the development folder and not the normal folder.
reload(picos.constraint)
Out[22]: <module 'picos.constraint' from '/home/optimi/bzffourn/python/lib/python2.7/site-packages/picos/constraint.pyc'>
while the source code are here:
/home/optimi/bzffourn/ZIB/python_scripts/pyMathProg/picos
So the changes I make are not considered.
This should help you do it: Override Python import order.
Just change your import to this:
import sys
sys.path.insert(0,"/home/optimi/bzffourn/ZIB/python_scripts/pyMathProg/picos")
import picos.constraint
Related
I'm having a hard time trying to import modules created inside a project.
The structure of my project is as follows:
myapp/calcs/__init__.py
myapp/calcs/calculations.py
myapp/tests/__init__.py
myapp/tests/test_calcs.py
Inside test_calcs.py, I have the following import statement:
from calcs import calculations as clc
However, I am getting this error:
ModuleNotFoundError: No module named calcs
I can't understand why I am having this error as I have included an init.py file inside calcs which should make it act as a module.
I also found this part of python extremely confusing. You basically need to make your project an installable package and do an editable install of it in your environment to import your modules this way. I would recommend this youtube video for a great walkthrough on the process, you only need to watch 2:36-8:36 for this topic specifically but the whole video is quite useful. Best of luck.
The first three answers in this thread describe how you can solve this pretty well!
Short version:
change from calcs import calculations as clc to from ..calcs import calculations as clc
start a terminal session in the parent folder of myapp
run python -m myapp.tests.test_calcs.py
I'm working on contributing open source to the pandas package inside python. When I run import pandas as pd, it points to the installed version of python.
Wondering the best way to import the local version of the pandas library that I forked from github and set break-points inside to understand how the different functions work.
You should take a look at the pdb-module, using it for break-points goes as simple as:
import pdb # import the module
pdb.set_trace() # set break-point & open interactive shell
you can use the set_trace method whereever you want in any Python script you'd like to debug.
Here's a link to the official docs:
https://docs.python.org/3/library/pdb.html
I want to install updated packages in another directory and have Python grab the updated package instead of the older package.
I'm trying to find a way that I can specify which directory to import for when there are multiple identical packages in sys.path.
I started by running this code to make sure that the path for the second module is present:
import sys
print('\n'.join(sys.path))
Both paths are shown, so I know that Python could find the package from either location.
I run this to see what path Python is using:
import statsmodels
print(statsmodels.__file__)
It is using the path of the out of date version.
I've been looking into using importlib but I haven't figured out how to make that work.
I'm just looking for a way to import a package from a specified path, even when the package exists in another directory in sys.path.
as discussed in the commend you'd neeed to implement this solution. With that to further explain what it does, it points to another folder to consider files to import. Considering the mentioned code:
# some_file.py (this is this script you're running)
import sys
sys.path.insert(0, '/path/to/application/app/folder')
import file_name_inside_the_folder_above
You'd let the first argument 0 untouched and just edit the second argument, pointing to which folder the script you have access is. Then you just import as the it's file name.
I have two google.protobuf modules on my Debian(stretch).
/usr/local/lib/python2.7/dist-packages/google
/home/myuser/.local/lib/python2.7/site-packages/google (installed with pip --user)
I'd like to import 2, but python always gives me 1, while ipython imports 2. I've tried set PYTHONPATH such that /home/myuser/.local/lib/python2.7/site-packages/ is the first in it, but not working.
Is there any way I can force python to search my $HOME/.local/lib/python2.7/site-packages/ first?
Yes. See here for an official description of how python determines which module to import first: https://docs.python.org/2/tutorial/modules.html#the-module-search-pathkk
See here for a way to change that default behavior: http://www.hasenkopf2000.net/wiki/python/how-override-pythons-module-import-order/
Of the two solutions provided on the hasenkopf site, the second will be less problematic if you change your mind on which module you want to use. You just edit the file rather than having to remember which symbolic links you created. Briefly, the code is:
import sys
# Assume path to module is
# /path/to/recent/version/of/module.py
sys.path.insert(0,"/path/to/recent/version/of")
import module
You place it at the top of your script.
I was believing that when importing a package, it would search from sys.path and use the first hit for import. However, it seems not true:
import mpl_toolkits
print(mpl_toolkits.__path__)
And it outputs:
['/Library/Python/2.7/site-packages/matplotlib-1.5.0-py2.7-macosx-10.11-intel.egg/mpl_toolkits', '/usr/local/lib/python2.7/site-packages/mpl_toolkits']
Can someone please explain to me how exactly python looks for packages if it is installed multiple times in the machine (in different location searchable by sys.path)? Or a pointer to relevant reference would be good.
when you import a module, python uses PYTHON PATH (system variable that contains a list of folders) and loops to search for importable module.
Python will test if it is a package (folder containing init.py) or a module (*.py). it will stop on first module found if no module is found python raises an import error