Cannot execute pip after changing virtualenv folder name - python

So previously I named my virtual environment "test". After that, I changed it to "testt", and after that I can't access pip commands anymore and it gives me the following error:
Fatal error in launcher: Unable to create process using '"C:\coding\test\test\Scripts\python.exe" "C:\coding\test\testt\Scripts\pip.exe" ': The system cannot find the file specified.
How can I fix this?

You should never rename a virtual Python environment. When creating the virtual environment, the path is hardcoded in several places (see the Scripts/activate* scripts for example).
You could try to replace the hardcoded paths in all files, but I'm not sure how good this works and if this changes between (Python/venv) versions.
Best thing to do is just remove the old virtual environment and create a new one.
If you're using a requirements.txt file, this as as easy as:
py -3.10 -m venv new_env
new_env\Scripts\python.exe -m pip install -r requirements.txt
(example commands are for Windows and Python 3.10, but are similiar for Linux and/or other Python versions)
If you didn't use a requirements.txt file, you could run pip freeze before removing the old virtual environment to see which modules you had installed.
Side note: this method has the advantage that you're actually verifying that your environment is well documented (i.e., the requirements.txt is up-to-date) and that you can reproduce it. This will make it easier to repeat this process in the future (e.g. on another computer) as well.

Related

How to run python project on Windows

I'm a mobile dev, no experience with backend environment. I'd appreciate some steps to config my Windows to run python projects, like this one: https://github.com/avilash/TikTokAPI-Python
I can't figure out how to config, run, and test those methods that are described. I assume it should be an IDE for python, but not sure which one is good for Windows, and how ton config it to run a project like this.
On mobile it's a lot easier, just download project, import in the IDE and run it. Any best practices for experienced devs are appreciated!
install python on your pc, then do pip3 install -r requirement.txt . I personally prefer vscode. Its the best with required extension installed, or if you are the newbie and you want to learn python, then don't install extension.
Download the standard Python distro from here https://www.python.org/downloads/. I would stick with versions 3.6 or 3.7 for now for stability and compatibility reasons.
Run the installer, the default installation path will be something like C:\Program Files\Python3X\. Since you're installing it for the first time, it will be your only installation, so you can choose during the install to set your environment variables (typically including PATH and PYTHONPATH) to point to this installation as the default one.
Create your project folder anywhere you like, where you can put your .py scripts. You can then cd into the folder in the command line and run any scipt like this python myscript.py (or if you haven't set the default env vars C:\Program Files\Python3X\python.exe myscript.py).
This is the bare minimum you need to be able to run Python projects.
To install the package you mentioned, in the command line type pip install git+https://github.com/avilash/TikTokAPI-Python (or C:\Program Files\Python3X\Scripts\pip.exe install git+https://github.com/avilash/TikTokAPI-Python). This will work because the project has a setup script (setup.py). Note that this will install it into the Python installation folder and you will NOT be able to modify it. If you want to be able to do development on it, clone it into a separate folder, navigate into it and install in 'dev' mode from the current folder using pip install -e . (note the dot!)
For the IDE, you can use VS Code if you're already using it, just install the Python extension. If you're more into IntelliJ, you can then use PyCharm which will have the familiar interface but it's a separate IDE. Both are similarly good in terms of features and maturity.
Other notes you can find useful at the beginning:
If you install multiple Python installation in different places, they are pretty much self-contained and defined by their installation path, but of course the one for which you will select to set the environment vars during installation will be the default one. If you want to run the pip command for a specific installation, then you will have to use C:\Program Files\Python3X\python.exe -m pip install ....
pip is Python package manager, typically you can install anything available on https://pypi.org/ with just pip install pandas for example.
You can check all packages installed using pip list. Since pip itself is also just a Python package, it will always be in the list as it's installed by default when you install Python.

How do I test out changes to a python library pulled through git?

I have a python library that I am wanting to help out with and fix some issues. I just don't know how to test my changes given the complexity of how python/pip installs libraries.
I have the library installed with pip and I can run python code connecting to the library by doing an "from import *". But now that I want to make changes to it I pulled the code with git and plan to branch to work on my changes. That's fine. I will then do a pull request to merge any changes given tests pass.
But after I make a change, how do I integrate my changes into python to test out the changes I made with the library? Can pip install my custom/modified version of the library?
I have looked around and haven't successfully found an answer to this but perhaps I'm not looking in the right spot.
Can pip install my custom/modified version of the library?
Yes.
There are various ways of approaching this question. A common solution is the use of Python virtual environments. This allows you to create an isolated Python environment that does not share the same packages as your system Python install. You can then install things into it (such as your modified Python library) to test it out.
To get started, you need the virtualenv tool. This is probably available as a package for your distribution, but you can also install it using pip. Once you have it, you can run in the same directory as your code:
virtualenv .venv
This creates a virtuelenv named .venv. You can call it anything you want, but naming it .venv (or anything starting with a .) means it won't clutter up the output of ls in your workspace.
Next, you need to activate the virtualenv:
. .venv/bin/activate.sh
This modifies your $PATH to place the virtualenv at the front of the list of directories. Now when you type python or pip, you'll be using the virtualenv version.
If your code has a setup.py file, you can install it like this:
pip install -e .
The -e means you want to perform an "editable" install, which means python will use the code "in place", and any changes you make will be immediately visible to the code you use for testing.
When you're done, you can run:
deactivate
This will remove the changes that activate made to your environment.
For more information:
Pipenv & Virtual Environments discusses a higher level tool for managing virtual environments.
Virtualenvwrapper is another take on a higher level management tool.

How do I set up a configured environment from a cloned repo?

I cloned a repo and I'm trying to run my tests and i'm getting an interpreter error:
Interpreter path does not exist: C:\Users\username\Source\Repos\citcodownloader\env\Scripts\python.exe
The project downloaded a .sln, solutions view that I opened it with and I thought it set up my enviornment but it doesn't seem to be doing so. Not sure what to do from here.
The best thing you can do is create a (or use an existing) Virtual Environment. It looks like your program is looking for one in the folder "env". Try this:
Open a terminal (Windows key + R, then type cmd and press enter)
Navigate to your repo folder using chdir C:\path\to\your\repo
Run the command env\Scripts\activate.bat (if there is no folder called "env" in your repo, use my instructions below)
Try running your program again.
I hope this helps, post a comment if it doesn't and I'll add as much detail or explanation as you need. Good luck!
For Googlers or people who that doesn't help, look for these files in your repo:
requirements.txt (a list of plugins you need to set up a virtual environment)
venv/ (folder containing a virtual environment)
Solution
If a folder named 'venv' or 'virtualenv' does NOT exist,
run this command to create it: python -m venv venv (or for Python 3: python3 -m venv venv). If it does exist, move forward.
You have a virtual environment! Now enter into it using: source venv/bin/active (on Unix or OSX, see the link above for the Windows command).
If requirements.txt is there, run this command next: pip install -r requirements.txt. If not, move forward.
Run the program again (via whatever method the repo says you should use). If you get 'error: module is not installed' use the command pip install moduleNameHere and run the program again.
Keep doing step 4 for each missing module, once the program is working use pip freeze > requirements.txt to create a requirements file and save yourself the headache next time. :)

How to move django project from ubuntu env to virtual env?

I have an existing django project which I have developed using python libraries installed in system and adding missing ones to the system. But now conflict has come for python-requests as system has 2.2 version but I need >2.5. Dont't want to uninstall and put newer one as it may break the OS. So now, I want to use virtual env and install packages there in complete isolation to that of OS.
I think the solution you're looking for is to download a different version of python without uninstalling your original, then start up virtualenv venv, but by passing in a path to the new python.exe file. Like this: virtualenv -p venv <path-to-executable-here>, then just do source bin\activate, as usual. This starts the virtual environment using the python executable you passed to virtualenv through the terminal.
Also, this might not be the only way, but, there's something called ModuleFinder which enables you to get a list of all the modules your script is importing--That is if you don't want to type them out manually, and you have extra modules installed (otherwise pip freeze > requirements.txt would do the job, and your new virtual environment would install all the packages in requirements.txt).

Move the virtualenvs to another host folder

By error, I forgot to specify the WORKON_HOME variable before creating my virtual environments, and they were created in /root/.virtualenvs directory. They worked fine, and I did some testing by activating certain environment and then doing (env)$ pip freeze to see what specific modules are installed there.
So, whe I discovered the workon home path error, I needed to change the host directory to /usr/local/pythonenv. I created it and moved all the contents of /root/.virtualenvs directory to /usr/local/pythonenv, and changed the value of WORKON_HOME variable. Now, activating an environment using workon command seems to work fine (ie, the promt changes to (env)$), however if I do (env)$ pip freeze, I get way longer list of modules than before and those do not include the ones installed in that particular env before the move.
I guess that just moving the files and specifying another dir for WORKON_HOME variable was not enough. Is there some config where I should specify the new location of the host directory, or some config files for the particular environment?
Virtualenvs are not by default relocatable. You can use virtualenv --relocatable <virtualenv> to turn an existing virtualenv into a relocatable one, and see if that works. But that option is experimental and not really recommended for use.
The most reliable way is to create new virtualenvs. Use pip freeze -l > requirements.txt in the old ones to get a list of installed packages, create the new virtualenv, and use pip install -r requirements.txt to install the packages in the new one.
I used the virtualenv --relocatable feature. It seemed to work but then I found a different python version installed:
$ . VirtualEnvs/moslog/bin/activate
(moslog)$ ~/VirtualEnvs/moslog/bin/mosloganalisys.py
python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
Remember to recreate the same virtualenv tree on the destination host.

Categories