Installing custom packages inside a python venv - python

I need to install some custom python tools into my python virtual environment. I can install them fine globally.
The venv was created by running python3 -m venv env
My files are laid out like this:
tool/
|-setup.py
project/
|-env/
|-|-bin/
I've installed globally by going to tool/ and running python3 setup.py this works great and I can then open a python terminal and type import tool and everything works.
So now I need to use it in the virtual env. First thing I tried was navigating to project/env/bin and running python3 ~//tool/setup.py which didn't throw errors but when I enter a python shell and try import tool I get the error ImportError: No module named 'tool'
Next I tried activating the venv with source evn/bin/activate navigating to tool/ and installing just like I would globally by running python3 setup.py which again throws no errors and gives a success message but doesn't work in a python shell.
My questions are: is this at all the correct way to install something into a venv when not using pip? and if so, what did I break and how can I fix my setup to work?

Related

How to run python script using keyboard lib as root and also have access to the packages on the project venv?

I have a python project which uses a specific venv. I tried to use the "keyboard" package on it, but I got the error "ImportError: You must be root to use this library on linux.". Then I installed the package with sudo python3.9 -m pip install keyboard and tried to run the script like sudo python3.9 path/to/script. It seems that the root problem was solved, but now I get ModuleNotFoundError: for every package I used on the script that was installed on the project's venv.

"ModuleNotFoundError: No module named 'kivymd'" in .spec file

I already did pip install kivymd in my Python project. I also had the kivymd directory in my project.
I'm working with a Mac.
I created a spec file called "coinsnack4.spec" including the code below:
from kivymd import hooks_path as kivymd_hooks_path
However, when I try to package my python project with the spec file with the command:
pyinstaller -y --clean --windowed coinsnack4.spec
I got the error below:
File "coinsnack4.spec", line 3, in <module>
from kivymd import hooks_path as kivymd_hooks_path
ModuleNotFoundError: No module named 'kivymd'
I really don't know why this happens because I already pip install kivymd. I don't know what to do next and I would appreciate if anyone could help me with this error.
Thank you very much!
Why are you facing this issue?
The reason behind this is the concept of virtual environments in python. Each virtual environment is independent of the other. You can use different virtual environments, activate and deactivate them as per your project's requirements.
I would suggest you go through this doc once Python venv
As when you do a `pip install <SOME_PACKAGE> from your local terminal, it installs the package from into the default python environment and from the terminal itself (not pycharm terminal) if you try to execute the python program it will work fine but as soon as you switch to pycharm or any other IDE, it has it's own python environment set and that environment is unaware of what happened in the other python environment. So you need to install the pip package here also, in order to execute the same python program.
Solution:-
The first thing I would suggest is to install the package in the virtual environment that the pycharm is using. For that click on the Terminal icon the below bar of your pycharm window. then do run the below command :-
python3 -m pip install kivymd
If this doesn't work, try configuring the python environment in pycharm.
Below is how you can change or update your python interpreter in pycharm: -
Setting an existing Python interpreter
At any time, you can switch your Python interpreter either using the Python Interpreter selector or in the Project Settings/Preferences.
Creating a new Python interpreter
To add a new interpreter to the current project:
If you have a conda environment, follow the below steps: -
Or if you want to setup a new virtual environment, do as below: -
I think you installed pyinstaller not in project's virtualenv, just:
pip install pyinstaller
then problem will be fixed.

Installed modules not found by Python

I need to use the autogui module to do something in Python. When I run the script, it says that it can't find the autogui module so I installed it with
pip install autogui.
But when I run the script again it still says me this module doesn't exist.
Method 1:
You're probably having trouble setting up your correct Python Interpreter and working within it,try the following in VSCode.
Ctrl + Shift + p
And enter the following in the field.
python: select interpreter
Select the desired environment and reinstall PyAutoGui
Method 2:
Creating a virtual environment for your project where all your packages will be installed and will be isolated from others, and will have no import errors since it's an environment specifically for the project that you're working on.
I assume you use Windows, so open the command line in your working directory, or open your working directory in VSCode and enter the following in the command-line tool that is provided within VSCode.
The Python installers for Windows include pip. You should be able to access pip using:
py -m pip --version
You can make sure that pip is up-to-date by running the following
py -m pip install --upgrade pip
Installing virtual environment
py -m pip install --user virtualenv
Creating a virtual environment
py -m venv env
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.
venv will create a virtual Python installation in the env folder.
Finally, to activate the environment run the following command
.\env\Scripts\activate
That will activate your environment.
pip install pyautogui
Make sure to change your interpreter to the one that you just created in the env/bin folder and run your code, or you could just enter the path to the *python file located in the env/bin folder.
Try pyautogui - I had the same problem. Instead of autogui, write `pyautogui. Or, if you are running python3 or higher then try:
pip3 install pyautogui.

How to import module installed by pip to a custom directory

I'm writing a plugin for an app, and found that packages I install with pip are apparently being ignored, so I'm trying to install them within my plugin's directory using
pip install --ignore-installed --install-option="--prefix=[plugins-folder]" [package-name]
This creates a folder structure lib/python2.7/site-packages/[dependencies] which is fine by me, except I can't figure out how to them import those dependencies. Even if I manage to import the main package, it will break because it can't find it's own dependencies which are also in that same directory structure.
I suggest that you should use virtual python environment. virtualenv for python2/python3 and python3 -m venv for python3.
You python environment seems orderless and you should isolate each environment for each python app.

virtualenv doesnt find installed module when running tests [Pytest]

I'm writing my first test for a class where I imported an external packages ( installed via pip in the venv ).
I'm using PyCharm as an IDE and it the package in question is listed there under the project interpreter ( the venv ) as well as when I type pip freeze in console.
Now I want to run a run-tests.sh file and when my test is reached pytest is returning me an ERROR : E ImportError: No module named 'magic'
The code line which fails the test is obviously the import statement in my class which I want to test.
Any ideas?
//Edit:
for clarification: NOT the terminal itself is throwing the Error! PYTEST does!
fixed it myself. for some dubious reason pytest and my venv did have a problem. reinstalled pytest within my virtual env via pip install pytest
make sure that you have installed packages through pycharm, if it don't list your package, it means that you install your package in other place, go to file > setting > project:[NAME] >interpreter to check it. then yo can use "+" to install package

Categories

Resources