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
Related
I created virtual environment and activate it.
installed packages but unable to import them from virtual environment.
pip freeze:
But getting error trying to import module:
Traceback (most recent call last):
File "z:\Documents\Python\Projects\ProjectName\tempCodeRunnerFile.py", line 1, in <module>
import paramiko
ModuleNotFoundError: No module named 'paramiko'
How to make sure .py file uses virtual environment?
Also if I run
import sys
print(sys.path)
Result:
'C:\\Users\\Username\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages'
So it does not uses virtual environment, is that correct?
In many cases it's not necessary to activate a virtual environment. Typically you could do something like the following from anywhere without activating the virtual environment:
C:\path\to\venv\Scripts\python.exe -m pip somecommand
C:\path\to\venv\Scripts\python.exe different\path\to\script.py
etc.
Additionally you could specify the absolute path to the python.exe as a she-bang at the top of your Python script, and execute the script directly (for example with a double-click) without calling Python explicitly.
#!/path/to/venv/bin/python3
print("Hello world")
References:
https://docs.python.org/3/using/windows.html#shebang-lines
https://docs.python.org/3/library/venv.html#creating-virtual-environments
You should invoke the python that is installed within your environment. That is
source myenv/bin/activate
python path/to/script.py
In Unix, you would add the following #!/usr/bin/env python at the top of your script and this would allow you to run it without specifying the python binary.
source myenv/bin/activate
path/to/script.py
Possibly that works in Windows in the same way or differently.
I am trying to do the following in Python 3.7.1 on Windows
import sqlite3
but I get the following error message
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "c:\programdata\anaconda3\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "c:\programdata\anaconda3\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
I have searched for a solution to the problem for quite a while now to no avail. I have also successfully run pip install pysqlite3 on the Anaconda prompt, but the import still fails. What do?
I got this working on windows by downloading: the sqlite3 dll (find your system version)
And placing it into the folder: C:\Users\YOURUSER\Anaconda3\DLLs
(Depending on how you installed Anaconda, this may have to be placed into
the following folder: C:\ProgramData\Anaconda3\DLLs)
According to #alireza-taghdisian, you can locate the exact path of
your conda environments (where you need to copy the sqlite3 dll) by typing:
conda info --envs on your anaconda prompt.
Locate the sqlite3.dll file. In my case it was in following folder
C:\Users\Admin\anaconda3\Library\bin
where C:\Users\Admin\anaconda3 is the folder where Anaconda was installed
Add this to PATH in environment variables, and it should work then.
Try copying the sqlite3.dll from the
C:\Users\YOURUSER\anaconda3\Library\bin
folder to
C:\Users\YOURUSER\Anaconda3\DLLs
Please check https://github.com/jupyter/notebook/issues/4332
I added anaconda root/Library/bin to my PATH and now it works!
Add CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1 to your environment variables.
before executing the program, enter conda activate in your shell.
I had tried all above solutions But for me and my system I got to know that
I downloaded Python in C:\Python27 hence there is dll folder in python C:\Python27\DLLs
I installed Sqlite3.dll in my above dll folder
May be this solution will help you because it completely depends on where do you install your python
Happy coding :)
I put the sqlite3.dll in the path folder of my Python venv and still wont work. I suspected it is a path problem.
(In my case: E:\Virtual_Env\mini_zinc\env\Scripts)
I found in my case I messed up installation in a virtual evn, somehow using an anaconda python kernel within a Python venv.
I reinstall the Python Venv and check the python version after installed Env is correct (not the Anaconda python), then proceed with Jupyter Notebook (or Juyterlab) and works fine.
I was able to resolve this issue by putting sqlite3.dll file in the C:\Users<USERID>\AppData\Local\conda\conda\envs<ENV NAME>\DLLs.
Download sqlite3.dll file from https://www.sqlite.org/download.html
or copy it from C:\ProgramData\Anaconda3\DLLs\
I found the #elgsantos useful. But for those who are new to Python and Conda like me, I would like to add a little bit of details.
1- I use miniconda3 for creating new environment.
2- interestingly, I got two installation path on my computer for conda: the first one (the obvious) is located on "C:\Users\taghdisian\miniconda3". The second one is on "C:\Users\taghdisian\AppData\Local\r-miniconda". The latter is the primary path that you need to copy your sqlite3 files into the envs folder. I copy them in the "C:\Users\taghdisian\AppData\Local\r-miniconda\envs\sdr3.9\DLLs" in which the sdr3.9 is one of my virtual Condo environment.
you can locate the exact path of your conda environments (where you need to copy sqlite3) by typing the conda info --envs on your anaconda prompt.
I hope this help.
got the same error while loading the jupyter notebook from other conda prompt than "base" environment.
[1]: https://i.stack.imgur.com/2DW7E.png
Resolved by installing sqlite package
(nlpenv) C:\Users\arunk>conda install sqlite
launching
*
(nlpenv) C:\Users\arunk>jupyter notebook
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.
I am starting to work with the Python Anaconda distribution from Continuum.io to do scipy work.
I have been able to get Anaconda up and running, but I cannot tell whether Anaconda creates a new PYTHONPATH environment variable for each new environment it creates, or whether it relies on the common system PYTHONPATH.
I could not find any information on this in the documentation.
Further, when I did a printenv, I did not see a PYTHONPATH variable in the newly created environment --though I did find a few new anaconda created environment variables.
The best I can find is that Anaconda added some Anaconda directories and the new environment directory to the head of PATH variable --but this does not necessarily isolate the new package from the system environment but it is close.
Does anyone know the answer to this question or found a way to deal with this concern?
Anaconda does not use the PYTHONPATH. One should however note that if the PYTHONPATH is set it could be used to load a library that is not in the anaconda environment. That is why before activating an environment it might be good to do a
unset PYTHONPATH
For instance this PYTHONPATH points to an incorrect pandas lib:
export PYTHONPATH=/home/john/share/usr/anaconda/lib/python
source activate anaconda-2.7
python
>>>> import pandas as pd
/home/john/share/usr/lib/python/pandas-0.12.0-py2.7-linux-x86_64.egg/pandas/hashtable.so: undefined symbol: PyUnicodeUCS2_DecodeUTF8
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/john/share/usr/lib/python/pandas-0.12.0-py2.7-linux-x86_64.egg/pandas/__init__.py", line 6, in <module>
from . import hashtable, tslib, lib
ImportError: /home/john/share/usr/lib/python/pandas-0.12.0-py2.7-linux-x86_64.egg/pandas/hashtable.so: undefined symbol: PyUnicodeUCS2_DecodeUTF8
unsetting the PYTHONPATH prevents the wrong pandas lib from being loaded:
unset PYTHONPATH
source activate anaconda-2.7
python
>>>> import pandas as pd
>>>>
No, the only thing that needs to be modified for an Anaconda environment is the PATH (so that it gets the right Python from the environment bin/ directory, or Scripts\ on Windows).
The way Anaconda environments work is that they hard link everything that is installed into the environment. For all intents and purposes, this means that each environment is a completely separate installation of Python and all the packages. By using hard links, this is done efficiently. Thus, there's no need to mess with PYTHONPATH because the Python binary in the environment already searches the site-packages in the environment, and the lib of the environment, and so on.
When I use the IPython included with Enthought Python Distribution, I can import the pyvision package just fine. However, when I try to import pyvision inside of PyCharm 1.2.1, I get the following errors
File "C:\Python27\lib\site-packages\pyvision\__init__.py", line 146, in <module>
from pyvision.types.img import Image,OpenCVToNumpy,NumpyToOpenCV
File "C:\Python27\lib\site-packages\pyvision\types\img.py", line 43, in <module>
import numpy
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 142, in <module>
import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 13, in <module>
from polynomial import *
File "C:\Python27\lib\site-packages\numpy\lib\polynomial.py", line 17, in <module>
from numpy.linalg import eigvals, lstsq
File "C:\Python27\lib\site-packages\numpy\linalg\__init__.py", line 48, in <module>
from linalg import *
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 23, in <module>
from numpy.linalg import lapack_lite
ImportError: DLL load failed: The specified module could not be found.
Am I missing some path settings in Windows?
I had the same problem. I'm using Winpython32 and trying to import win32com. Worked everywhere (I tried) except in PyCharm. sys.path and os.environ['PYTHONPATH'] had some extra entries inside Pycharm, but nothing is missing compared to when run elsewhere.
The solution was to start Pycharm within the Winpython console and not using the shortcut.
sys.path and os.environ['PYTHONPATH'] did not change. os.environ['PATH'] had several additional entries set, all related to the python installation. At this point I suspect it has to do with "non-standard" installations. Winpython32 tries to be "portable", while other reports of similar problems are when using Enthought or Python(x,y).
Manually adding:
C:\WinPython-32\python-2.7.6\
C:\WinPython-32\python-2.7.6\DLLs
C:\WinPython-32\python-2.7.6\Scripts
to the system path (the global PATH environment variable in Windows) solved the problem without having to run Pycharm within the Winpython command line. Note: C:\WinPython-32\python-2.7.6\Scripts alone did not solve it.
I've had that problem before, and it seemed to get fixed by repairing Enthought.
EDIT: I just checked, one of my f2py projects was still suffering from this exact error. Repairing Enthought did not work. The solution to my problem actually lay in fixing the Windows path variable. You need to make sure c:\Python27\Scripts (or your equivalent) is in the path. Furthermore, and this is VERY important, make sure each entry in the global and user path environment variables has NO trailing slashes. This breaks the GNU make utility on Windows.
Add to your PATH environment variable
C:\Python27
C:\Python27\DLLs
C:\Python27\Scripts
This is a pretty frustrating bug in PyCharm. Even if you set your virtualenv from within PyCharm, the "python console" defaults to the system python. When you installed PyCharm, presumably you used a win32 python on a 64 bit machine.
Go to file>settings>Build, Execution, Deployment>Console>Python Console and change the Python Interpreter from the system version to your virtualenv.
Of course, PyCharm doesn't immediately refresh it. You have to close your project and reopen it.
To verify this was successful, open the Python Console (Tools>Python Console) and check the very first line of the output: it should point to the python.exe of your virtual environment, not the system python.
I have the same problem (with another package) and I don't want to modify windows path.
I could solve it very roughly in the pycharm console, by creating a python script which updates sys.path and os.environ['PATH'] in file -settings-console-pathon console - starting script.It works only when I use "tools- python shell"
Still it doesn't work in the run options. I even tried to create a virtual environment, add the path to the activate.bat but in pycharm it doesn't work (from command prompt it does) (as suggested in virtualenv that can find relocated libraires (like mysqlclient lib for MySQLdb) )
I explicilty set the path in the python interpreter option but it doesn't work.(as suggested in https://stackoverflow.com/a/24206781/1136458)
I couldn't find the link but I saved this pic - hopefully works for you
I apologize for my explanation being long and probably not the best of clarity, but this is the best I could do to describe my experience.
I was having the same problem after first installation and this is how I solved it:
I had noticed there are some settings, as indicated in other answers, that tell pyCharm which interpreters and environment managers to use and I was sure the problem was with setting these options, but I was not sure how, so I started searching.
Setting interpreter
I had more or less followed the standard tutorial, assuming in some point I would have used newly created virtualenv inside my project folder. I was initially getting the DLL error, but as I made sure the interpreter settings were pointing at a valid executable.
Here it is explained how to do this.
Setting environment
at this point the error disappeared, but I had progressed towards a new error.
The problem at this point I was not being able to import any module as they were not found.
This is because I was working inside the newly created virtualenv, basically a pristine installation, with no modules installed. I am sure there are more evolved solutions (install modules in the virtualenv), but I was just looking for making the code to work, so I set the environment to use my anaconda usual development environment.
For me, with conda on windows (after activating the development environment with conda activate) then (see ..../anaconda-python-where-are-the-virtual-environments-stored for other systems).
where python gives the path to the interpreter
conda info --envs gives me the existing environment that I want to use.
This fixed everything for me.