Just working my way through a (very good) book call Test Driven Development using Python.
This makes use of Python3.4 by the way. By the way, I am running in a Windows 7 OS.
I've got all the stuff working using a simple text editor and running from the command line... in the course of which in particular I used "pip install" to install Django and Selenium, as per book's instructions.
This created folders "selenium" and "django" under ...\Python34\Lib\site-packages\ ... so I added these to the PythonPath for my Eclipse/PyDev project.
With the correct interpreter selected I then tried to run a file which runs fine on the command line: "> python3 functional_tests.py"... but I get
File "D:\apps\Python34\lib\site-packages\django\http\__init__.py", line 1, in <module>
from django.http.cookie import SimpleCookie, parse_cookie
File "D:\apps\Python34\lib\site-packages\django\http\cookie.py", line 5, in <module>
from django.utils.six.moves import http_cookies
ImportError: cannot import name 'http_cookies'
... to me this looks like a dependency thing... as though "pip install" handles dependency matters in a way just including a single folder doesn't.
Question boils down to this: what's the "proper" way to install a python module using PyDev?
several days later
wow... nothing? Nothing! I suppose this must mean that you either have to add dependencies manually or use something like Ant, Maven or Gradle within Eclipse itself. These latter are not my strong areas, even outside an IDE. Would still be nice to have an answer from a PyDev expert!
Well, pip install should work for PyDev (it should automatically recognize the dependency)...
I.e.: in your use case, the only folder that should be in the PYTHONPATH is D:\apps\Python34\lib\site-packages (and pip should install packages to that folder -- make sure you don't add extra folders for "D:\apps\Python34\lib\site-packages\django" nor anything else inside the site-packages to the PYTHONPATH).
If it's still not working, please check if the module django.utils.six.moves.http_cookies is indeed where you expect it to be. Also, you can print the PYTHONPATH being used in runtime with:
import sys
print('\n'.join(sorted(sys.path)))
To check if that's really what you expect.
Related
I'm on day 1 of Python and trying to import SciPy into a project. I installed it via pip install on ElementaryOS (an Ubuntu derivative). I have verified it's existence via:
$ python
>>> help("modules")
The exact error I'm getting is:
Import "scipy" could not be resolved Pylance (reportMissingImports)
When searching for this error I found:
Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10 Powershell -- the accepted answers all pointed towards a project specific .env file. I have no such project structure, nor does it make sense to me that one would be needed.
A github issue -- this issue ends with "it just fixed itself"
When I run my program, I get no errors in console. And looking up "Pylance" it appears to be a Microsoft product. I suspect that VSCode is failing to lint correctly. Potentially because pip installed something in a place it wasn't expecting. This is my guess, but any help would be very much appreciated.
Edit: Following through on the idea of missing paths, I found this post -- How do I get into the environment VS Code is using for pylance?
Having added the path to where my modules can be found has yielded no results, though I'm not sure if the formatting is correct. Perhaps it needs glob syntax (eg path/**/*)
The issue was indeed with Pylance. It was missing an "additional path" to where pip had installed the projects I wanted to import. To solve the issue:
First make sure you know the location of your import; you can find it with:
$ python
>>> import modulename
>>> print(modulename.__file__)
Then, once you know the location:
Open settings (ctrl + ,)
Search "pylance" or find it under "Extensions > Pylance"
Find the "Extra Paths" config item
Use "add item" to a add a path to the parent folder of the module. It will not do any recursive tree searching
And you should be good to go!
For a further example, you can see the image above where I had added the path /home/seph/.local/lib/python2.7/ to no avail. Updating it to /home/seph/.local/lib/python2.7/site-packages/ did the trick.
I'm on day 1 of Python and trying to import SciPy into a project. I installed it via pip install on ElementaryOS (an Ubuntu derivative). I have verified it's existence via:
$ python
>>> help("modules")
The exact error I'm getting is:
Import "scipy" could not be resolved Pylance (reportMissingImports)
When searching for this error I found:
Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10 Powershell -- the accepted answers all pointed towards a project specific .env file. I have no such project structure, nor does it make sense to me that one would be needed.
A github issue -- this issue ends with "it just fixed itself"
When I run my program, I get no errors in console. And looking up "Pylance" it appears to be a Microsoft product. I suspect that VSCode is failing to lint correctly. Potentially because pip installed something in a place it wasn't expecting. This is my guess, but any help would be very much appreciated.
Edit: Following through on the idea of missing paths, I found this post -- How do I get into the environment VS Code is using for pylance?
Having added the path to where my modules can be found has yielded no results, though I'm not sure if the formatting is correct. Perhaps it needs glob syntax (eg path/**/*)
The issue was indeed with Pylance. It was missing an "additional path" to where pip had installed the projects I wanted to import. To solve the issue:
First make sure you know the location of your import; you can find it with:
$ python
>>> import modulename
>>> print(modulename.__file__)
Then, once you know the location:
Open settings (ctrl + ,)
Search "pylance" or find it under "Extensions > Pylance"
Find the "Extra Paths" config item
Use "add item" to a add a path to the parent folder of the module. It will not do any recursive tree searching
And you should be good to go!
For a further example, you can see the image above where I had added the path /home/seph/.local/lib/python2.7/ to no avail. Updating it to /home/seph/.local/lib/python2.7/site-packages/ did the trick.
I am experience this annoying error message, every time after I have updated my module and try to reload it.
I do have a module mymodule in a package mypackage that has a __init___.py file in it.
When I do
from mypackage import mymodule
everything is ok.
After I update the module and reload it with
reload(mymodule)
Error pops up:
In [4]:
...: reload(constants)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-264a569b44f9> in <module>()
1
----> 2 reload(mymodule)
ImportError: No module named mymodule
To resolve this, I have to kill my interpreter and re-import everything when I want to reload one module, which is extremely time-consuming and annoying. How may I fix it?
PS:
I suspect this is something wrong with PYTHONPATH, but since I am using Python tool for Visual Studio, I cannot find the PYTHONPATH option.
Update
As far as I remember, it seems that things start going wrong immediately after I have this
import os
os.chdir(constants.PROJECT_PATH + '//data//')
in one of the modules. Yet does it really matter?
I don't think it matters, as the path in the brackets is exactly my project path.
Try it:
import os, sys
my_lib_path = os.path.abspath('../../../mypackage')
sys.path.append(my_lib_path)
from mypackage import mymodule
or add your package into PYTHONPATH. For unix it:
$ export PYTHONPATH=/absolute/path/to/mypackage
Is your package in the present working directory?
When the interpreter comes across an import libraryname statement, it looks for libraryname in several locations: the present working directory, directories specified by the PYTHONPATH environment variable, and some installation dependent paths.
So as long as your module is in the present working directory, the interpreter is able to find it. However, once the pwd changes, the interpreter isn't able to find the module anymore, and the import fails. You really have two options:
Install your module in a location where Python can find it. Typically, Python packages are located in /usr/lib on Linux systems (not sure about Windows, but you could easily find out). If you put your package there, then the interpreter will pick it up. I wouldn't recommend doing this by hand; write a simple setup.py script to handle the installation for you.
Tell Python where to explicitly look for the package. This is usually done through the PYTHONPATH environment variable. Set that at the command line before invoking the interpreter (or at the system-level, if you must).
If you can't change PYTHONPATH for some reason, then you can modify the path during runtime:
import sys
sys.path.append(your_directory_here)
This is a pretty ugly way to deal with the problem, so should be a last resort.
Whenever I try to import a file into python, it comes up with this error(or similar):
Traceback (most recent call last):
File "C:/Python33/My Files/username save.py", line 1, in <module>
import keyring.py
ImportError: No module named 'keyring'
I am trying to create a password storing program, and I was looking up for good ways to keep passwords secure, and someone said use import keyring, so I did, except, it never works. I must be doing something wrong, but whenever I look anything up for python, it never works out for me. It's almost as if loads have things have been changed over the years.
and idea's?
The keyring module is not part of the Python standard library. You need to install it first. Installation instructions are included.
Once installed, use import keyring, not import keyring.py; the latter means import the py module from the keyring package. Python imports should use just the name of the module, so not the filename with extension. Python can import code from more than just .py python files.
I was getting the same error "ModuleNotFoundError: No module named 'keyring'". And after installing this module pip install keyring, the same error occured with another module name. Then I came to the conclusion that it is the fact that the VSCode is not able to connect to my venv, even after setting the Python Itereptor. Press CTRL + SHIFT + P, and then type Python: Select Interceptor, and select your venv, if you want to set this.
To fix the issue, I had to force the VSCode to use the .venv I created, and luckily there is a dropdown to do that, on the top right corner as in the preceeding image. Click ont the Python version, and then you will be able to select your virtual environment.
Now it will take the modules from your virtual environment.
The only thing I can get python omnicomplete to work with are system modules. I get nothing for help with modules in my site-packages or modules that I'm currently working on.
Once I generated ctags for one of my site-packages, it started working for that package -- so I'm guessing that the omnicomplete function depends on ctags for non-sys modules.
EDIT: Not true at all.
Here's the problem -- poor testing on my part -- omnicomplete WAS working for parts of my project, just not most of it.
The issue was that I'm working on a django project, and in order to import django.db, you need to have an environment variable set. Since I couldn't import django.db, any class that inherited from django.db, or any module that imported a class that inherited from django.db wouldn't complete.
I get completion for my own modules in my PYTHONPATH or site-packages. I'm not sure what version of the pythoncomplete.vim script you're using, but you may want to make sure it's the latest.
EDIT: Here's some examples of what I'm seeing on my system...
This file (mymodule.py), I puth in a directory in PYTHONPATH, and then in site-packages. Both times I was able to get the screenshot below.
myvar = 'test'
def myfunction(foo='test'):
pass
class MyClass(object):
pass
Just ran across this on Python reddit tonight: PySmell. Looks like what you're looking for.
PySmell is a python IDE completion helper.
It tries to statically analyze Python source code, without executing it, and generates information about a project’s structure that IDE tools can use.
While it's important to note that you must properly set your PYTHONPATH environmental variable, per the the previous answer, there is a notable bug in Vim which prevents omnicompletion from working when an import fails. As of Vim 7.2.79, this bug hasn't been fixed.
Trouble-shooting tip: verify that the module you are trying to omni-complete can be imported by VIM. I had some syntactically correct Python that VIM didn't like:
:python import {module-name}
Traceback (most recent call last):
File "<string>", line 1, in ?
File "modulename/__init__.py", line 9
class empty_paranthesis():
^
SyntaxError: invalid syntax
Case-in-point, removing the parenthesis from my class definition allowed VIM to import the module, and subsequently OmniComplete on that module started to work.
I think your after the pydiction script. It lets you add your own stuff and site-packages to omni complete.
While your at it, add the following to your python.vim file...
set iskeyword+=.
This will let you auto-complete package functions e.g. if you enter...
os.path.
and then [CTRL][N], you'll get a list of the functions for os.path.