Importing custom modules (.py files) into virtual environment - python

I created different virtual environments due to different dependencies requirements, one of which is running Python 2.7. I have three .py modules included in a folder, that folder is in the same directory as all my scripts. However, when I try import those three .py modules I got an error "ImportError: DLL load failed: The specified module could not be found.". I suspect that it is due to the Python Interpreter in VSCode being pointed towards my Python 2.7 virtual environment in which these three .py modules do not exist. My questions are :
How can I "install" these three modules into my virtual environment ?
Is there a quick way to install these three modules into all virtual environments ?
Thank you for your help.

You can create a setup.py, then install this folder as a module by pip
pip install -e /path/to/folder
See more at https://packaging.python.org/tutorials/packaging-projects/

Related

New modules not imported

After updating macOs to Monterey, every new modules I install with pip won’t be imported in my Python programs. My sis.path are good and all the packages I need are in the site-packages. Can somebody help me ?
You need to check your python environment.
try which pip3 or which python3 to check locate your active python directory.
You can have different python versions installed in macOS which often leads to this issue.
Or maybe you are using virtual environments which creates isolated site-packages. If this is intended, then you should run your script within this environment to use the packages that you've installed.

PyInstaller with Anaconda Environment Not Packaging Pip Modules

So, I'm running PyInstaller according to the Kivy documentation in a dedicated Conda environment.
Only one module wasn't available through Anaconda, so I had to download it via Pip. Once my .spec file packages the .exe, running it in the terminal throws a ModuleNotFoundError for only that Pip-installed module. I didn't see a place in the .spec file to include just that module, either.
Is there a way to forcibly include the module (plus any of its dependencies) into a .spec file, or is there a way to port a Pip-installed module directly to the Conda environment so that PyInstaller can spot it during packaging?
In my case, the easiest solution was just to take the module's code base and load it directly into the project.

Can't import numpy module into python file, but it works with terminal

On Ubuntu I have python 3.6.9
I created new project using PyCharm, into project folder there is venv folder.
When I open venv folder in terminal and do:
python3
import numpy
It works, no any error.
But when I create some file in that venv folder using PyCharm, and try in this file:
import numpy
then I get ModuleNotFoundError: No module named 'numpy' error.
Why can't import module into file? module obviously installed because I can import that using terminal (folder is same for both cases).
What is my mistake ?
Pycharm uses virtual environments for serving modules, so you will have to download the package from either the GUI or by activating the virtual environment in the terminal and then doing the pip install.
if you want to do it from the GUI you can check the official tutorial. (Working and tested)
To do it manually, first to activate the environment in linux go wherever the venv folder is and type source venv/scripts/activate (to activate) and then you'll be able to do pip install numpy. (Not tested but it should work)
Could you share your Pycharm configuration?
Some extra steps are needed in Pycharm to configure venv, otherwise it will just take your normal interpreter site-packages.
Take a look at the Pycharm help page: https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html#existing-environment
looks like you need to install numpy through pip3.
Also, check which version of python is running in pycharm.

Spyder - python - install external packages

I have just started to use python (within Windows, 64bit) - and I have a basic question on how to install external packages within the anaconda / spyder environment. I understand that for most packages one can simply use “conda install bunnies”. However, certain packages are not in the anaconda repository, and might have be installed externally (e.g. from github). For those packages, in order to have spyder to recognize this package – does one only in addition have to update the PYTHONPATH manager in Spyder to include the directory (e.g. c:\users\bunnies) in which one has downloaded this package? Or should one take additional steps / is there a faster way?
You have several options to use packages that are not (yet) available via conda install:
1.) If the respective package is on PyPi you can build it as described in the manual.
2.) If building from scratch doesn't work and the package is on PyPi you can also try an installation via pip. Not that you have to use the pip in your Anaconda distribution and not the one of your systems Python installation.
3.) If you want to include external packages or local folders that contain Python-scripts you can do the following.
3.1.) Use the sys module and append the required package/folder to the path:
import sys
sys.path.append(r'/path/to/my/package')
3.2) Or put the modules into into site-packages, i.e. the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages which is always on sys.path. (Source)
3.3) Or add a .pth file to the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages. This can be named anything (it just must end with .pth). A .pth file is just a newline-separated listing of the full path-names of directories that will be added to your path on Python startup. (Source)
Good luck!

Install custom modules in a python virtual enviroment

I am doing some pylons work in a virtual python enviorment, I want to use MySQL with SQLalchemy but I can't install the MySQLdb module on my virtual enviorment, I can't use easyinstall because I am using a version that was compiled for python 2.6 in a .exe format, I tried running the install from inside the virtual enviorment but that did not work, any sugestions?
Ok Got it all figured out, After I installed the module on my normal python 2.6 install I went into my Python26 folder and low and behold I happened to find a file called MySQL-python-wininst which happened to be a list of all of the installed module files. Basicly it was two folders called MySQLdb and another called MySQL_python-1.2.2-py2.6.egg-info as well as three other files: _mysql.pyd, _mysql_exceptions.py, _mysql_exceptions.pyc. So I went into the folder where they were located (Python26/Lib/site-packages) and copied them to virtualenv's site-packages folder (env/Lib/site-packages) and the module was fully functional!
Note: All paths are the defaults

Categories

Resources