This is my first time using virtualenv and MySQLdb and I'm getting a strange error. After I setup this virtualenv, I installed MySQLdb from within the virtualenv (with the ENV actually activated). MySQLdb is not installed globally. When I'm in my ENV folder, open a python terminal, and try to import the module I get the following:
import MySQLdb
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named MySQLdb
However, when I do the same thing from the site-packages folder
...ENV/lib/python2.7/site-packages
import MySQLdb
everything seems to work just fine.
Also, when I run
help ('modules')
from either directory, I get very slightly different lists (even though it's telling me I'm using the same python executable). It's like for some reason the same python executable is getting it's modules from different places and the only time it's getting the modules it's supposed to have is deep inside the ENV, in this case the site-packages folder. If I'm not in the site-packages folder, it seems to get a different list and I get no access to MySQLdb.
I'm using Mac OSX Mavericks, XCode is up to date to the best of my knowledge (I don't find I use it very much).
I used this to setup the virtualenv
virtualenv -p /usr/local/Cellar/python/2.7.8/bin/python ENV
which python gives me this both in ENV and in site-packages
...ENV/bin/python
Versions
virtualenv version 1.11.6
python 2.7.8 (not the default python)
UPDATE:
Installed sqlalchemy
(ENV)tims-mbp:ENV timbauer$ pip install sqlalchemy
And am getting the exact same affect. It seems like even though it's using the correct python when inside ENV, that version of python only is getting the correct module list when it's directly inside the site-packages folder. Otherwise it looks like it's pulling from the global list
Found the issue. Inside ENV/bin/pip there was a line that said this
#!/Users/timbauer/Desktop/ENV/bin/python2.7
That needed to say this
#!/Users/timbauer/Desktop/ENV/bin/python
Switching the above out and restarting terminal seems to have fixed everything. I had to delete the virtualenv and reinstall everything to get it to work, but I'd just barely started so this wasn't really that big of a problem.
Related
I want to be able to import FreeCAD into my python scripts, but only in one conda virtual environment. Is there a way to do this without adding FreeCAD to the path at the beginning of each file? I am using Pop!_OS, which should behave like Ubuntu here.
I already found that you can import FreeCAD, but the source I found did so by appending the FreeCAD library location at the beginning of the file: https://www.freecadweb.org/wiki/Embedding_FreeCAD. It looks like you could circumvent this problem by modifying your path variable, and I was able to do so on Windows in my workplace. I just want to do this only for a particular conda virtual environment.
Ideally,
import FreeCAD
will work in a special virtual environment, but not in others.
As mentioned, I got the import statement to work on Windows already by adding FreeCAD's directory to the PATH environment variable. It worked with the default python in command prompt, which should be the anaconda installation, so I think it works in all virtual environments. On Linux, though, I cannot import FreeCAD in python even when I use
PATH=$PATH:/usr/lib/freecad-python3/lib/
which I got from "locate FreeCAD.so" . I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'FreeCAD'
It would be really nice to be able to run the same code on both operating systems, and have the PATH modification confined to one virtual environment.
Conda does not look for packages from the PATH environment. Check this answer for the details. But first check whether your package can be installed using pip or conda.
You might go to the virtualenv site packages dir and add the path to the freecad into easy_install.pth
(Solution given at the bottom)
I downloaded the module requests in the command prompt using 'pip install requests'. It downloads fine and works from the command prompt, but when trying to import from the IDLE PyCharm I get the error message:
Traceback (most recent call last):
File "C:/Users/asher/PycharmProjects/begginer_questions/decode_a_web_page.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
By copy-pasting the 'requests' file into the location:
C:/Users/asher/PycharmProjects/begginer_questions
along with the files contained in the file path:
C:/Users/asher/PycharmProjects/begginer_questions/venv/Lib/site-packages/pip-19.0.3-py3.7.egg/pip/_vendor
the code shown below finally gave no error messages.
However, this only seems like a temporary fix. Does anyone know of a permanent fix that could work or any reasons as to why this is happening? The two common problems which often cause this to occur is that multiple versions of Python are downloaded and there is already a file called requests.py, neither are the case for me. I also don't understand why I had to copy-paste the second bunch of files considering they're already contained in begginer_questions.
I'm on Windows 10 with Python 3.7.3
import requests
url = 'https://www.nytimes.com'
r = requests.get(url)
r_html = r.text
Solution:
After a long while I found the answer so for anyone struggling with this problem I hope this helps.
My confusion came from the fact that when a project is created with up to date pip and Python, a virtual environment (venv) is automatically created and is a primary place where the project will look for modules/packages. In the command prompt navigate to your project - in my case it would be:
> cd User\asher\PycharmProjects\begginer_questions
Then enter the following command the activate the virtual environment "venv" pre-made by Python or Pycharm (note this is different for Mac):
venv\Scripts\activate.bat
In this virtual environment enter the command:
pip install requests
The solution seems (and probably is) very obvious, but this should work because sys.path will always look for packages in this virtual environment instead of where ever it was installed when I tried before. Don't forget to deactivate your virtual environment in the command prompt when you're finished installing whatever packages you want by entering:
deactivate
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.
Having some weird troubles installing python modules on my work computer (read: no admin/root rights), I'm using 2.7.5. I downloaded and unpacked the tarball and ran 'setup.py', but it had no effect: When I open the python shell, it can't find the module (this specific one is fuzzywuzzy). However, if I right click -> edit with IDLE the setup.py, and then run the shell from that file, it loads and works perfectly fine. Or, if I then open a new file from that shell, use the module and run it, it works fine. -__-
I've tried using:
import sys
sys.path.append('path here')
to append the location where the module is installed, but this doesn't help, nor does the path stay in the sys.path list when I close/reopen the shell.
This is actually driving me insane. Can anyone help? I'm relatively new to programming and python.
The best and easy way provided by python to install/uninstall packages is to use PIP.
use this
python -m pip install packagename==version
same way to uninstall
python -m pip uninstall packagename==version
if you are using windows you need to set path variable first usually python file will be in path C:\Python27 to set path variable
PATH=%PATH%;C:\Python27;
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