I have windows 10 with 2 user accounts, one is an old one that has Python installed. My latest account also has Python installed. I used pip install tweepy, and it now exists in the site packages. If I open my Python shell (v3.6.5), it imports fine. However, I can't get this to work when switching to Python in cmd. In here, after typing python, it says Python 3.6.6. Obviously they are different, yet they seem to be originating from the same directory.
Any ideas?
[edit] I have just realised that I have an external application that runs on Python 3.6.6., so I guess it's defaulting because of that. However, I can't uninstall that since I need it - is there a way to specify which version of Python is launched when I type python into the cmd?
[edit2]: This is the issue, and the solution there works. However, I want to be able to just type python into cmd, rather than the entire path. Since both exist in the path (and I don't want to remove the other 3.6.6.), is there a way to achieve this?
Have you thought about using a virtual environment?
https://virtualenv.pypa.io/en/latest/
When you enter "python" into your cmd it searched python.exe in the directories listed on your PATH variable.
All you need to do is to modify your PATH and add the directory path in which the python with the desired version is located.
I have downloaded the rpy2 package using MacPorts, but every time I try to import it using my IDE (Enthought Canopy version 1.5.4), I get the following error:
ImportError: No module named rpy2
However, when I run Python in my terminal and run import rpy2 there, it imports without any issue.
I did some googling on the issue, and it appears that Python's environmental variables in the terminal are not the same as the Python's environmental variables in the IDE, particularly the PYTHONPATH. I checked my PYTHONPATH in my terminal by running import sys; sys.path and got the following file paths:
/usr/local/lib/python2.7/site-packages
/Users/bob
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Users/bob/Library/Python/2.7/lib/python/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2-2.8.6-py2.7-macosx-10.6-intel.egg
The very last file path points to the location of the module that won't be imported in my IDE.
I then ran the same commands on a script in my IDE and got the following file paths:
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python27.zip
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-darwin
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-mac
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-mac/lib-scriptpackages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-tk
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-old
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-dynload
/Users/bob/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/Users/bob/Library/Python/2.7/lib/python/site-packages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/extensions
/Users/bob/.ipython
The location of rpy2 module is not present in the above block of file paths. So, it seemed logical to me to simply add its file path to the PYTHONPATH environmental variable.
However, an answer to this post (Using MacPorts to install modules via a certain path) mentions that it is potentially dangerous for the following command to be carried out:
sys.path.insert(-1, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2-2.8.6-py2.7-macosx-10.6-intel.egg')
Can anybody support/gainsay this claim? Any additional explanation would be most helpful.
UPDATE
So, it turns out, it actually is BAD for the script. I decided to try running some examples from the tutorials, and the kernel just crashes every time, and must be restarted. How can I remedy this?
SECOND UPDATE
After following the advice set out in How to: Macports select python , I decided to run the following command in the terminal in order for MacPorts to select the version of Python that was being used by my Canopy IDE:
sudo port select --set python python27
This seems to have solved the issue, as my kernel does not crash anymore when I run basic commands from the rpy2 module. But I remain skeptical as to whether this is a long term solution or not, as I am still confused behind the discrepancies of installing a module via MacPorts versus the IDE's package manager.
I'm working on a project for which I need to a module. I want to know how to import some other module in python that is not installed.
When I write
import xaut
It gives error no module named xaut.
I have xaut-0.4.5 that I downloaded from following link.
Please help me how to use it.
Well I think following will help
Extract your zip file
Open the command line (a linux terminal or cmd on windows). I am on linux so I have a terminal.
Now enter the directory that you have extracted.
In it is a directory "python" cd into it.
If you run the ls command (if on windows run dir) you will see that in this directory there is a script "setup.py". We need to execute it for installation.
Execute this script by giving command python setup.py install
This will hopefully install it and then you would be able to import it.
Basically you have only downloaded the package. To make a package work you also need to install it. So when you download a package always search for a setup.py file.
This is certainly a duplicate question. You can look up how to add a python module to the path in windows.
here is an example
How to add to the pythonpath in windows 7?
I'm trying to run a script that launches, amongst other things, a python script. I get a ImportError: No module named ..., however, if I launch ipython and import the same module in the same way through the interpreter, the module is accepted.
What's going on, and how can I fix it? I've tried to understand how python uses PYTHONPATH but I'm thoroughly confused. Any help would greatly appreciated.
This issue arises due to the ways in which the command line IPython interpreter uses your current path vs. the way a separate process does (be it an IPython notebook, external process, etc). IPython will look for modules to import that are not only found in your sys.path, but also on your current working directory. When starting an interpreter from the command line, the current directory you're operating in is the same one you started ipython in. If you run
import os
os.getcwd()
you'll see this is true.
However, let's say you're using an ipython notebook, run os.getcwd() and your current working directory is instead the folder in which you told the notebook to operate from in your ipython_notebook_config.py file (typically using the c.NotebookManager.notebook_dir setting).
The solution is to provide the python interpreter with the path-to-your-module. The simplest solution is to append that path to your sys.path list. In your notebook, first try:
import sys
sys.path.append('my/path/to/module/folder')
import module_of_interest
If that doesn't work, you've got a different problem on your hands unrelated to path-to-import and you should provide more info about your problem.
The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules. Editing or setting the PYTHONPATH as a global var is os dependent, and is discussed in detail here for Unix or Windows.
Just create an empty python file with the name __init__.py under the folder which showing error, while you running the python project.
Make sure they are both using the same interpreter. This happened to me on Ubuntu:
$ ipython3 -c 'import sys; print(sys.version)'
3.4.2 (default, Jun 19 2015, 11:34:49) \n[GCC 4.9.1]
$ python3 -c 'import sys; print(sys.version)'
3.3.0 (default, Nov 27 2012, 12:11:06) \n[GCC 4.6.3]
And sys.path was different between the two interpreters. To fix it, I removed Python 3.3.
The main reason is the sys.paths of Python and IPython are different.
Please refer to lucypark link, the solution works in my case. It happen when install opencv by
conda install opencv
And got import error in iPython, There are three steps to solve this issue:
import cv2
ImportError: ...
1. Check path in Python and iPython with following command
import sys
sys.path
You will find different result from Python and Jupyter. Second step, just use sys.path.append to fix the missed path by try-and-error.
2. Temporary solution
In iPython:
import sys
sys.path.append('/home/osboxes/miniconda2/lib/python2.7/site-packages')
import cv2
the ImportError:.. issue solved
3. Permanent solution
Create an iPython profile and set initial append:
In bash shell:
ipython profile create
... CHECK the path prompted , and edit the prompted config file like my case
vi /home/osboxes/.ipython/profile_default/ipython_kernel_config.py
In vi, append to the file:
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/home/osboxes/miniconda2/lib/python2.7/site-packages")'
]
DONE
Doing sys.path.append('my-path-to-module-folder') will work, but to avoid having to do this in IPython every time you want to use the module, you can add export PYTHONPATH="my-path-to-module-folder:$PYTHONPATH" to your ~/.bash_profile file.
This is how I fixed it:
import os
import sys
module_path = os.path.abspath(os.getcwd() + '\\..')
if module_path not in sys.path:
sys.path.append(module_path)
Before installing ipython, I installed modules through easy_install; say sudo easy_install mechanize.
After installing ipython, I had to re-run easy_install for ipython to recognize the modules.
If you are running it from command line, sometimes python interpreter is not aware of the path where to look for modules.
Below is the directory structure of my project:
/project/apps/..
/project/tests/..
I was running below command:
>> cd project
>> python tests/my_test.py
After running above command i got below error
no module named lib
lib was imported in my_test.py
i printed sys.path and figured out that path of project i am working on is not available in sys.path list
i added below code at the start of my script my_test.py .
import sys
import os
module_path = os.path.abspath(os.getcwd())
if module_path not in sys.path:
sys.path.append(module_path)
I am not sure if it is a good way of solving it but yeah it did work for me.
I have found that the solution to this problem was extensively documented here:
https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/
Basically, you must install the packages within the Jupyter environment, issuing shell commands like:
!{sys.executable} -m pip install numpy
Please check the above link for an authoritative full answer.
This kind of errors occurs most probably due to python version conflicts. For example, if your application runs only on python 3 and you got python 2 as well, then it's better to specify which version to use.
For example use
python3 .....
instead of
python
Had a similar problem, fixed it by calling python3 instead of python, my modules were in Python3.5.
This problem occurs due to the different versioning - e.g. if the Python installed on your machine is installed in a folder called path_to_lib/python3.6 but your notebook is running in Python 3 - the spacing in the naming matters!
How to solve it?
When creating a new jupyter notebook, just choose the Python with the same versioning as yours (watch out for spaces!). See the attached image.
I found yet another source of this discrepancy:
I have ipython installed both locally and in commonly in virtualenvs. My problem was that, inside a newly made virtualenv with ipython, the system ipython was picked up, which was a different version than the python and ipython in the virtualenv (a 2.7.x vs. a 3.5.x), and hilarity ensued.
I think the smart thing to do whenever installing something that will have a binary in yourvirtualenv/bin is to immediately run rehash or similar for whatever shell you are using so that the correct python/ipython gets picked up. (Gotta check if there are suitable pip post-install hooks...)
Solution without scripting:
Open Spyder -> Tools -> PYTHONPATH manager
Add Python paths by clicking "Add Path".
E.g: 'C:\Users\User\AppData\Local\Programs\Python\Python37\Lib\site-packages'
Click "Synchronize..." to allow other programs (e.g. Jupyter Notebook) use the pythonpaths set in step 2.
Restart Jupyter if it is open
This is probably caused by different python versions installed on your system, i.e. python2 or python3.
Run command $ pip --version and $ pip3 --version to check which pip is from at Python 3x. E.g. you should see version information like below:
pip 19.0.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
Then run the example.py script with below command
$ python3 example.py
Happened to me with the directory utils. I was trying to import this directory as:
from utils import somefile
utils is already a package in python. Just change your directory name to something different and it should work just fine.
This answer applies to this question if
You don't want to change your code
You don't want to change PYTHONPATH permanently
Temporarily modify PYTHONPATH
path below can be relative
PYTHONPATH=/path/to/dir python script.py
import sys
sys.path.append('/Users/{user}/Library/Python/3.7/lib/python/site-packages')
import ta
If anyone comes across this issue using conda with Jupyter Notebook in MSVS Code, the solution is to make sure you're using the correct kernel. The kernel is in a box in the top right corner of the interface and looks like this:
I pointed mine to the version of Python that also matched my application path -- problem solved!
This is what worked for me: I just changed my working directory inside my notebook
import os
os.chdir("my/path/to/module")
os.getcwd()
I have a similar issued with my Jupyter Lab setup which I resolved by checking the Jupyter Lab log on opening. This informed me that the virtual environment (pipenv) couldn't locate the Jupyter Lab so it used a shared version (from an earlier installation of Python).
I created a requirements.txt and discovered I hadn't installed Jupyter Lab in this new environment. Installing it resolved the import error.
Remove pathlib and reinstall it. Delete the pathlib in sitepackages folder and reinstall the pathlib package by using pip command:
pip install pathlib
I'm pretty new to python and twisted and I tried to run a simple twisted script but failed.
My environment:
MacOX 10.7.2 + Python 2.7.1 + Twisted 11.0.0 + eclipse + PyDev
The script called test.py:
from twisted import reactor
reactor.run()
I tried to run it in the terminal and everything works fine.
Then I opened eclipse and created a new PyDev project then adding a py file called test.py and
typed the code above in.
When I tried to run it I got errors like:
Traceback (most recent call last):
File "/Users/user/Documents/workspace/TwistedDemo/test.py", line 2, in <module>
from twisted import reactor
ImportError: No module named twisted
Then I checked the PYTHONPATH and added the "twisted source folder" in the external libraries tab but the errors was still there.(nothing changed)
Hope someone can help me out here, thanks in advance :)
Several things to try here -
Since your env is Mac OSX. I installed commandline tools from Xcode which solved this problem. Xcode 4.4 and later install Command Line Tools
For the sake of completeness. If this on Ubuntu, then apt-get install python-twisted generally works.
from your eclipse put this in your python script -
python -c 'import sys;print sys.path'
This tells all the paths that python looks for when you import something. If you don't find twisted path there then add it to this like so - sys.path.append(twisted_dir_path)
Finally, if all the above does not help. type -p python in a shell will tell you which version you're using; I would mostly be /usr/bin/ or some variant of it.
Then /usr/bin/python2.7 -c 'from twisted.internet.protocol import Factory,Protocol' ought to succeed.
This has happened to me so many times, but I figured out previously. Basically, if you have too many versions of python on your mac, Aptana Studio for some reason gets confused, so make sure that your twisted installation is using the same python as the command line (even though you already added twisted to Aptana). I'll get into more specifics later
Ok, I think this is how you do it:
Grab the python path of the python that has twisted installed correctly, using this
import sys
print sys.path
And just grab the base path, nothing to specific.
Go to Aptana-preferences-PyDev-Interpreter-Python
And then just add a new interpreter (in my case I just called it python1 and set is the first one. If that doesn't work then just set a new PYTHONPATH right below it.
You need to install incremental before twisted
pip install --upgrade incremental
pip install Twisted
This should solve the problem.
Not sure how you installed twisted, did you try easy_install or pip, or install in manually?
If you want to make sure it's installed properly, try opening up a terminal, typing python, and then "import twisted". If it comes back without errors, then it's installed. You might need to set the PYTHONPATH variable to include the source folder.
Then, since you're using pydev in eclipse, you need to refresh your configuration each time you add a new library.
On the mac, this is under preferences -> PyDev -> Interperter-Python
I find it works best to remove my configuration and re-add it, for it to pick up everything. But you can click on new folder (select the folder with the init.py), or the new egg, if it's an easy_install egg (a .egg file is a zip file, if it's unzipped, you'll see a EGG-INFO folder in the subdirectory of the folder you want to select).
You should set the correct PYTHONPATH in Eclipse as following:
right click your project.
select Properties
select pyDev-PYTHONPATH
add your twised in the external libraries