python module not working in PyCharm with virtualenv - python

Currently i have a virtualenv created with the virtualenvwrapper. In that virtualenv i installed the cx_Oracle extension with pip install cx_Oracle.
I have a python script using several commands from cx_Oracle like connect and such.
When running my script thought the activated env (python script.py) it works fine and produces no errors.
But when i try to run the same script in PyCharm 4 it does not work. I have the virtualenv as intrepeter selected. When running the script i get an error as follows:
/Users/pgerrits/.virtualenvs/siebelaudit/bin/python3.4 -u /Applications/PyCharm.app/Contents/helpers/pydev/pydev_run_in_console.py 64420 64421 /Users/pgerrits/PycharmProjects/SiebelAudit/Audit/Siebel Audit/scratchpad.py
Running /Users/pgerrits/PycharmProjects/SiebelAudit/Audit/Siebel Audit/scratchpad.py
PyDev console: starting.
ImportError: dlopen(/Users/pgerrits/.virtualenvs/siebelaudit/lib/python3.4/site-packages/cx_Oracle.so, 2): Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
Referenced from: /Users/pgerrits/.virtualenvs/siebelaudit/lib/python3.4/site-packages/cx_Oracle.so
Reason: image not found
When running the same script with the same command in the terminal with the env activated, i get no error.
I already tried the following:
- Added ENV variables for oracle_home, etc using a script
- added env variables using the pycharm env variables option
It is really annoying that i have to switch to my mac terminal for running and debugging. Has anyone a clue what could be the issue here?

I had to set the environment variables for ORACLE_HOME
DYLD_LIBRARY_PATH and LD_LIBRARY_PATH
and restart PyCharm to get cx_Oracle to work.

This is a known issue of PyCharm. The only way is to create virtualenv using PyCharm. If you create with virtualenvwrpper, there's a chance that PyCharm won't recognize it.

import os
import platform
if platform.system() == 'Darwin':
os.environ["ORACLE_HOME"] = '/opt/oracle/instantclient_11_2'
os.environ["DYLD_LIBRARY_PATH"] = '/opt/oracle/instantclient_11_2'
os.environ["LD_LIBRARY_PATH"] = '/opt/oracle/instantclient_11_2'

Related

Module can be imported in python but cannot be used from command line

This is essentially the opposite of this problem: Import module works in terminal but not in IDLE
If I start a python session and try to import flask, then it works just fine. Despite that, if I just run flask from the terminal, it is not recognized.
I have confirmed that the locations which pip installs to is on my system path. (I'm on Windows 10 and I typed $Env:Path into Powershell to see that, for example, C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages is in the system path.)
I am also sure that I did pip install --user flask not inside a virtual environment. When I run import flask; print(flask.__file__ I see C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages)
Any other ideas for what could be going wrong?
Edit: I'm trying to run flask run. To check the path python uses, I did (in python) import sys; for i in sys.path: print(i)
As Python language based on Python interpreter, before you use Python, it is necessarily to activate the Python env.

PIP install packages but some not found

Some pip installs don't work on a computer, since recently I changed to Python 3.
for example, pywinauto
I used CMD:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts>pip install pywinauto
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts>pip freeze
It shows:
pywinauto==0.6.8
But when I:
import pywinauto
It gives:
ModuleNotFoundError: No module named 'pywinauto'
I moved the 2 folders "pywinauto" and "pywinauto-0.6.8.dist-info" from:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
To:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts\
And:
import sys
sys.path.append('C:\\Users\\NAME\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\')
It still doesn't work. (it works for some other cases)
Windows 64 settings seem ok with: Environment Variables > User variables
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
Environment Variables > System variables
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
p.s. I have Anaconda installed in the machine, and no Python 2. Not sure whether it causes the problem.
Go to your conda terminal, activate the virtual environment.
pip install <your package name>.
Now go to your editor where you're running this code. (like pycharm or Sublime)
select your python interpreter as venv (Anaconda). Usually this option is available in project setting.
Run your code, it should work now.
Seems the problem relates to Anaconda.
Problem solved by run CMD:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts>pip instsall pywinauto
Then manually moved the 2 folders "pywinauto" and "pywinauto-0.6.8.dist-info" from:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
To:
C:\Users\NAME\AppData\Local\Continuum\anaconda3
The package functions normally.

Installing Python packages for Visual Studio Code

I'm trying to import a package called "termcolor" that can run on VSC. I know the import is working because when I run the program from my mac terminal it executes fine. However, when I run from VSC, I get the ImportError "no module named termcolor".
I know it has been installed in the my virtual environment where I am building the program:
Kaylas-iMac:Ermes Marana rahme$ source "/Users/rahme/Desktop/Programming/Python/Ermes Marana/env/bin/activate"
(env) Kaylas-iMac:Ermes Marana rahme$ python3 -m pip install termcolor
Requirement already satisfied: termcolor in ./env/lib/python3.7/site-packages (1.1.0)
And I think I'm importing from the correct path. I saw in another post to use sys.path.append() to explicitly look for packages in the correct path, so I tried that. Here is my code:
import sys
sys.path.append('users/Rahme/Desktop/Programming/Python/Ermes\ Marana/env/lib/python3.7/site-packages')
import termcolor
print(termcolor.colored(text = "I'm Rahme", color ="red", on_color="on_grey"))
sys had a warning about the space in the directory /Ermes\ Marana and suggested adding an r to the backslash which I did and nothing changed. What am I doing wrong?
How can I install packages on VSC in the future so that I can use the built in run in terminal?
Just because you switched to your virtual environment in the mac terminal doesn't mean that the VSC terminal is operating in that virtual environment. To get VSC to know you want it to run in your environment, you need to hit ctrl+shift+p to pull up the command pallette. Then you type python: select interpreter. You will get a list of all the virtual environments that VSCode can see and you just select your environment from that list. Once you do that, VSC should operate fine inside your environment.
try
sys.path.append(r'users/Rahme/Desktop/Programming/Python/Ermes/
Marana/env/lib/python3.7/site-packages')
What is the purpose of the space?
Edit: keep the space if it is part of the folderpath
Please do see the r I have added infront of the folderpath

Cloud9 Python: getting Flask module not found on Run action

I started an empty Ubuntu (Ubuntu 14.04.3 LTS) workspace on cloud9. Python 2.7.6 was there by default. I installed python-pip and python-virtualenv (using apt-get).
Having created the smallest possible Flask application I've faced a problem: I get an import error if I press ide's Run button:
Update
The same problem here: https://community.c9.io/t/not-able-to-run-python-file-which-has-a-import-statement-for-flask-installed-via-virtualenv-py/6151
If I run the application from terminal by issuing python app.py then everything's fine:
Virtual environment is activated.
pip list shows
...
Flask (0.12) - it's there.
...
which python shows
/home/ubuntu/workspace/env/bin/python
What's wrong with my setup?
If running from the terminal after activating the environment works fine maybe you could include the command to activate the virtual environment from within the script, for example:
import os
os.system("source env/bin/activate")
import flask
you may need to use an absolute path for the environment but that should be easy enough to find

Virtualenv and python - how to work outside the terminal?

When I enter my virtual environment (source django_venv/bin/activate), how do I make that environment transfer to apps run outside the terminal, such as Eclipse or even Idle? Even if I run Idle from the virtualenv terminal window command line (by typing idle), none of my pip installed frameworks are available within Idle, such as SQLAlchemy (which is found just fine when running a python script from within the virtual environment).
Tell Eclipse or Idle that the python interpreter is django_venv/bin/python instead of /usr/bin/python
If you want your virtualenv to be really permanent to your project, you could stuff the following two lines directly into your code:
activate_this = 'this_is_my_project/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

Categories

Resources