I often have a collection of .py files, maybe split over a few folders, that all use some little special purpose library I've written that doesn't belong in my general PYTHONPATH. What's the cleanest way for me to get that library into the Python path for those scripts that need it? I'll be the only one ever to run any of these, and I will generally be doing so from Spyder.
I've tried with .pth files. This may not be the best way, but I can't get them to work regardless. For example, if I create a .pth file containing
/var
The running the following code out of the same directory via Spyder, I get output of True, False, True:
import sys
import site
print any(["python" in path for path in sys.path]) # True
print any(["/var" in path for path in sys.path]) # False
site.addsitedir(".")
print any(["/var" in path for path in sys.path]) # True
Here are the details of my environment:
Python 2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Nov 11 2013, 10:49:09)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Imported NumPy 1.7.1, SciPy 0.13.0, Matplotlib 1.3.1
Related
Problem solved.
If you add your path to PYTHONPATH, you can import the packages inside that path. But what I am doing is try to import that path, this is wrong.
So in this case, I make a sub Dir at my path and that dir is now a package can be imported.
Still I have to include my path in file>settings>project structure as source.
==========================================================================
I am using python3.6 in anaconda, Ubuntu16.04.
I have my own package in path /home/gph/pyutils_gph.
Inside this dir are utils.py files. I have include this path in PYTHONPATH.
I can do
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyutils_gph import utils
>>>
in terminal. But the same import code shows error in pycharm. It can find my package, indicating it with red lines.
What else should I do to make pycharm know my own package?
==========================================================================
I opened the terminal inside pycharm, get output like below. I have that dir in PYTHONPATH, but I can not import it. What's wrong?
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/gph/pyutils_gph', '/home/gph/Desktop/BorderSecure/detection_cnn/src', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python36.zip', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6/lib-dynload', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6/site-packages']
>>> from pyutils_gph import utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pyutils_gph'
>>>
If you have /home/gph/pyutils_gph in PYTHONPATH and you do from pyutils_gph import utils then it is looking for
"/home/gph/pyutils_gph/" + "pyutils_gph/utils.py`
because from pyutils_gph import utils means pyutils_gph/utils.py and it adds it to every path from PYTHONPATH
You have to add to PYTHONPATH
/home/gph
and then it will give
"/home/gph/" + "pyutils_gph/utils.py`
so you get correct path
First version can works in terminal if you run Anaconda in folder /home/gph because Python search packages also in current working directory so it finds pyutils_gph/utils.py directly in /home/gph without using PYTHONPATH
If you will go to different folder then it will not find local pyutils_gph/utils.py and you get the same error.
When I run the python3 command, nothing happens. No stdout, no stderr... nothing. When I try the python2 command, however- I get the expected result. What is going on?
ubuntu#ip-172-91-23-255:~$ python3
ubuntu#ip-172-91-23-255:~$
ubuntu#ip-172-91-23-255:~$ which python3
usr/bin/python3
ubuntu#ip-172-91-23-255:~$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Just solved the same issue
If you renamed the python executable change it back, else, ignore this.
add the folder of python3 instalation to the PATH,
the instalation folder is possibly
this: C:\Users\RV420\AppData\Local\Programs\Python\Python37-32
To add folders to your path you can just search 'environment
variables' on windows, then click on environment variables and find
one called path
Now make sure that the new folder comes first in the path that the Python2 instalation folder
You're good to go
I compiled VTK 7.0 (6.3 has the same effect) over cmake with following params:
-LIBRARY_OUTPUT_PATH:PATH="" -CMAKE_INSTALL_PREFIX:PATH="/usr/local" -VTK_ENABLE_VTKPYTHON:BOOL="1" -Module_vtkPython:BOOL="1" - -VTK_Group_Qt:BOOL="1" -CMAKE_OBJCOPY:FILEPATH="/usr/bin/objcopy" -VTK_RENDERING_BACKEND:STRING="OpenGL2" -VTK_INSTALL_PYTHON_MODULE_DIR:PATH="/usr/local/lib/python2.7/site-packages" -DVTK_EGL_DEVICE_INDEX:STRING="0" -VTK_WRAP_PYTHON:BOOL="1" -Module_vtkGUISupportQtOpenGL:BOOL="1"
Now i can find the binary "vtkpython" at /usr/local/bin .
Good news:
I am allowed to enter python shell with the command "vtkpython" out of this directory (/usr/local/bin) with all the needed vtk bindings.
markovich#markovich-desktop:~$ cd /usr/local/bin/
markovich#markovich-desktop:/usr/local/bin$ vtkpython
vtk version 7.0.0
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
>>> vtk
<module 'vtk' from '/usr/local/lib/python2.7/site-packages/vtk/__init__.py'>
>>>
Thats a bit irritating because i am expecting to run my default python environement and the vtk bindings are available.
so the bad news:
if I type python in my shell or vtkpython from another location on my system the shell says "no modulen named vtk found" on calling import vtk .
markovich#markovich-desktop:~$ vtkpython
vtk version 7.0.0
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named vtk
>>>
Question 1: Maybe I missed something in the make configuration ?
Question 2: If I take the actual status (which is somehow working): Is it possible integrate the "vtkpython" bindings in my default python environment? If I am not totally wrong. The binding is correctly loaded out of my python2.7 path like you can see in the terminal:
<module 'vtk' from '/usr/local/lib/python2.7/site-packages/vtk/__init__.py'>
So how can I add this module to be loaded in Python environment?
Since loading vtkpython clearly shows that you have the module available somewhere on your system, you should be able to add the location of the vtk module to your PYTHONPATH variable.
Find where the vtk module is installed (try /usr/local/lib/python2.7/site-packages, you should see a /vtk folder). If you're unsure, you could try to find it in vtkpython with
import vtk
import imp
imp.find_module('vtk')
You can check what paths are stored in PYTHONPATH by entering in terminal:
echo $PYTHONPATH
(In my install, it was empty by default.)
Then you can add the vtk folder location to your PYTHONPATH in terminal:
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
Check if vtk is availabe:
$ python
>>> import vtk
If it works, you can add the export... line above into your ~/.bashrc or ~/.profile (as appropriate per your distro installation) to permanently load the option in PYTHONPATH.
I'm struggling with Python paths. I want to be able to import a file from a directory. I can do this when it's available from the filesystem, but not when I'm relying on my PATH (update: or PYTHONPATH) to find it.
Here are the symptoms. First of all, check that my current directory is on my PATH:
(.venv)~/Dropbox/gds/myapp $ pwd
/Users/me/Dropbox/gds/myapp
(.venv)~/Dropbox/gds/myapp $ echo $PATH
... :/usr/local/go/bin:.:/Users/me/Dropbox/gds/myapp
And check that test/test_app is where I think it is (and has an __init__.py in the directory:
(.venv)~/Dropbox/gds/myapp $ ls tests/
__init__.py __pycache__ test_app.py test_views.py
Now check that Python can import test/test_app from this directory:
(.venv)~/Dropbox/gds/myapp $ python
Python 3.4.2 (default, Jan 29 2015, 13:46:46)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tests
>>> import tests.test_app
>>> exit()
All good so far. Now move to a different directory, and see if I can import test/test_app:
(.venv)~/Dropbox/gds/myapp $ cd ..
(.venv)~/Dropbox/gds $ python
Python 3.4.2 (default, Jan 29 2015, 13:46:46)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tests
>>> import tests.test_app
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'tests.test_app'
But /Users/me/Dropbox/gds/myapp is on my PATH, and I can import the directory, so why can't I import this file?
UPDATE:
Thanks for the suggestions. Just tried doing export PYTHONPATH=$PYTHONPATH:/Users/me/Dropbox/gds/myapp: but still see the same problem when I run Python.
The directory needs to be in PYTHONPATH, not PATH (which is for regular non-Python command lookup).
There is probably another tests Python package in sys.path. Check tests.__file__ to confirm. To fix it, use more specific names e.g., myapp.tests instead i.e., move tests package inside myapp Python package.
Python doesn't use PATH. It uses the PYTHONPATH variable to look up modules:
Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows).
PATH is a variable used by your shell to look up binaries; only the format (how paths are specified and separated) is shared.
I'm using the OpenID module as part of a flask application.
I've been looking in the module directory to see which methods are accessible, but was wondering if it's possible to see the code/logic behind the methods themselves. If so, how?
is there a similar command to dir(oid) for this?
You can usually find the source file for any given module by looking at the help output.
For example:
Python 2.7.6 |Anaconda 1.9.1 (x86_64)| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> help(collections)
Help on module collections:
NAME
collections
FILE
//anaconda/lib/python2.7/collections.py
...
From here you can open the file and view the source.
You can either have a look at your python installation/virtualenv "lib" folder
For example Flask src can be found at:
(pythondir)\Lib\site-packages\flask
Or online:
flask
flask-openid