No pyvenv.cfg file - python

I made a variable cfg = waterot.cfg and then made a new file "waterot.cfg" in pycharm, this automatically made a pyvenv.cfgfile, I didn't know what this was and I was getting errors with my code I deleted the pyvenv.cfg file and changed from .cfg to .txt. Now when I run my code I get an error No pyvvenv.cfg file
This is the problematic piece of code
redeemkey = message.author
if redeemkey.startswith('!download'):
if message.channel.id == 695834994064490658:
with open ('waterOT.txt', 'rb') as otcfg:
await message.author.send(file=discord.File(otcfg, 'waterOT.cfg'))
await message.delete()

In my case I tried many ways, but finally I do one thing that solve my error.
I just delete existing venv directory and set Project Interpreter from my editor.

If you know the path for the python distribution used to create the virtual environment, and it's version, you can just create yourself a pyvenv.cfg file with following contents
home = <location-of-python-exe>
include-system-site-packages = false
version = <version>
where
<location-of-python-exe> is the folder where the python.exe is located that was used to create the python virtual environment. Example C:\Python\Python385-64.
<version> is the version of the python, at <location-of-python-exe>. You can check it by running <location-of-python-exe>\python.exe. Example: 3.8.5.
Valid locations for the pyvenv.cfg are (PEP405):
Next to the python executable (venv/Scripts/pyvenv.cfg), i.e. in the same folder with python.exe.
One directory up from the python executable; root of the virtual environment (venv/pyvenv.cfg)
where venv is the assumed name of the virtual environment (could be something else, too).

With PyCharm you can restore that change in a couple of clicks:
Right-click anywhere in the editor and choose Local History | Show History from the context menu.
In the dialog that opens, the left-hand pane shows a list of all saved revisions of the current file with timestamps. The right-hand pane shows a diff viewer which displays the differences between each revision and the current state of the file.
OR
Do one of the following:
To revert the whole file or directory to the state of this revision, right-click it and choose Revert from the context menu or click the Revert icon on the toolbar.
To restore a specific code fragment, select the revision that contains that fragment. In the diff view on the right locate the piece of code you want to restore click the chevron button the Chevron button to copy it from the left pane.

The easiest way is to chose 'file' in left hand corner of pycharm window, then choose restart IDE, then follow the steps of restarting settings of IDE. It worked for me.

pyvenv.cfg is a configuration file for a virtual environment. Without knowing how you set things up, I assume Pycharm made the virtual environment.
You should not delete the pyvenv.cfg file if you want the virtual environment to work.

Related

PyCharm error: Cannot run program, error=2, No such file or directory

I am getting the following error message when attempting to execute Python code in PyCharm:
Cannot run program "/Users/x/.virtualenvs/untitled/bin/python" (in directory "/Users/x/PycharmProjects/untitled"): error=2, No such file or directory
I made sure everything was updated and restarted my computer, but I still get the same error. I have no idea what the problem is.
Edit
I just opened my terminal and was faced with this error message:
virtualenvwrapper_run_hook:12: no such file or directory: /usr/local/bin/python3.7 virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenvwrapper has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.7 and that PATH is set properly.
I have no idea what happened here. I certainly didn't touch any of this.
Edit 2
If I execute Python3 --version, I get Python 3.8.5.
Edit 3
I followed this, but this error remains:
Edit 4
This is the current state:
I think this is related.
If it helps at all this is what my venv settings looks like.
I don't have the answer as to why it happens, but I find its usually when renaming the project.
In the past i've recreated the project and copied the project files directly from the old folder to the new one in a file explorer (not pycharm) and its fixed it.
This Error Occurs because when you rename the file you need to update the file path name also...
.......Solution...........
click on Edit Configurations (near the green 'run' play button)
Where it says 'Script Path' at the end of the string is where you find the name of
your file
Change the name to the updated file name
Probably the root library has been deleted or corrupted.
I suggest to delete the venv library in explorer and then write the following commands in the terminal
> C:\Users\MY\PycharmProjects\pythonProject>py -m venv venv
> C:\Users\MY\PycharmProjects\pythonProject>cd venv\Scripts
> C:\Users\MY\PycharmProjects\pythonProject\venv\Scripts>activate
For me, it was the symbolic link in the project. Running this command fixed it:
xcode-select --install
You may want to check the contents of the 'activate' script inside your venv. It contains a few hardcoded, absolute paths to files and directories within your environment. If you change something in that environment, e.g. move a file or directory around, these paths can easily become invalid.
Similar issues can arise when you, for example, create a virtual environment inside a virtual machine, then open files in PyCharm from outside that machine.
For me, the problem was that I created the virtual environment through the PyCharm settings and ticked "Inherit global site-packages", I think. Once I removed the virtual environment and created it using the old fashioned way, i.e.
mkvirtualenv projectname --python=python3.10
everything fell into place.

How to get terminal to default open into my virtual environment?

I've created a virtual environment for Python and VSCode, but everytime I open up the terminal, rather than opening up to the work environment folder, it opens up to the folder where I keep my .py file.
Let me explain my steps.
I've created a Work folder, then a work environment folder inside of it. C:\Work and C:\Work\work_env
Next I've created a folder in C:\Work\myprojects to place my .py files in.
Now when I open up the terminal in VSCode, it runs the Activate.ps1 file and my work environment is ready to use but ONLY when I navigate into my work environment folder using cd in the terminal.
Is there a way to default it so that the terminal is open to the work environment right away? I followed all the steps in this short tutorial and this guy got his terminal to work that way but it's different for me. https://www.youtube.com/watch?v=Wuuiga0wKdQ
Thanks!
Add the following line to your settings.json:
"terminal.integrated.cwd": "DESIRED/PATH"
You can find more details here.

How to get Spyder to open python scripts (.py files) directly from Windows Explorer

I have recently installed the Anaconda distribution on Windows 7 (Anaconda 3-2.4.0-Windows-x86_64). Unlike IDLE, I can't right-click and open a py file in the Spyder IDE. I will have to open Spyder first and then navigate to the file or drag and drop it in the editor. Is there any way to open the file in the editor directly from Widows Explorer?
With the current version of Anaconda (4.1.0) you can simply right-click on a python script in Windows File Explorer and choose "Open with". The first time you do this you need to select "Choose default program" and then browse to spyder.exe in the Script directory in your Anaconda installation. Also make sure that the "Always use the selected program to open this kind of file" is unchecked and then click OK. From now on spyder.exe will always be listed as one of the options when you select "Open with" from the right-click menu in Windows File Explorer.
I have had a similar problem with another piece of software that I use.
My work around for this problem is to set the file association for .py files to C:\Anaconda\Scripts\spider-script.py via the Open with dialog. If you now try to open your File.py by double clicking you'll receive an error like
~\file.py is not a valid Win32 application.
This can be resolved by editing the spyder-script.py registry key:
HKEY_USERS\S-1-5-21-3559708500-1520960832-86631148-1002\Software\Classes\Applications\spyder-script.py\shell\open\command
and replacing the default value "C:\Anaconda\Scripts\spyder-script.py" %1 with "C:\Anaconda\python.exe" "C:\Anaconda\Scripts\spyder-script.py" %1. Use the search function for this key if the path isn't the same for your machine, and of course use the appropriate path for your python installation. spyder-script.py should now execute in a python shell.
From the docstring of ftype,
...Within an open command string, %0 or %1 are substituted with the file name being launched through the association.
(Spyder maintainer here) This functionality is available as part of our Windows installer. In other words, if you install Spyder with it, then you'll see a new entry in the Open with menu of the Windows Explorer that allows you to open Python files directly on Spyder.
Unfortunately, it's not possible for us to do the same for other installation methods (i.e. when using Anaconda or pip). That's why it's necessary to resort to different hacks, as the ones mentioned in other answers here, to have this functionality for them.
What is working very well for me in Windows (10), is associating the *.py files with a batch file (let's say "SpyderBATCH.bat") containing this line :
[ANACONDA_FOLDER_PATH]\pythonw.exe" "[ANACONDA_FOLDER_PATH]\cwp.py" "[ANACONDA_FOLDER_PATH]" "[ANACONDA_FOLDER_PATH]/pythonw.exe" "[ANACONDA_FOLDER_PATH]/Scripts/spyder-script.py" %1
Where [ANACONDA_FOLDER_PATH] has to be replaced with the full path to the Anaconda folder (usually under "Program Files").
What Windows does, when double-clicking on a python script (let's say "file.py"), is pass to SpyderBATCH, as parameter number %1, the full path to "file.py".
Then Spyder is launched and displays the script "file.py" in the editor view.
I figured I would post my solution for this as well.
I have Spyder installed in multiple different environments. You can't simply call the spyder-script.py script without errors, because the environment must be activated.
#echo off
call [YOUR_CONDA_PATH]\Scripts\activate.bat [YOUR_CONDA_PATH]
call conda activate [YOUR ENVIRONMENT]
call start [YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\pythonw.exe "[YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\Scripts\spyder-script.py" %1
You can remove the second line and remove the environment extension from the third line if you have Spyder installed in your base environment.
Hopefully for anyone experiencing any weirdness with the other solution, this one will do the trick by activating the environment correctly.
The solution from JoeB152 worked perfectly for me!
If you are interested in adding the spyder icon (or any other) to the .py-files and if you would like to avoid the cmd-pop-up, I found out the following workaround which is feasible without admin rights:
Download the portable version of Bat To Exe Converter (I used v3.0.10).
Open your custom .bat-file in the Bat to Exe Converter.
In the options settings, activate "Icon" and give the path to the respective icon (for me it's in: .../AppData/Local/Continuum/anaconda3/Scripts/spyder.ico).
Set Exe-Format to Invisible (no empty cmd window would pop up anymore)
Convert your .bat-file to an .exe-file.
As usual, set to open .py-files with the newly created .exe.
Enjoy!
Environment:
Windows 10,
Conda 4.8.2,
Spyder 4.0.1,
Python 3.7
This problem is related to anaconda installation defaults - it does not register itself in PATH by default an dicourages users to do so. After proprly registering all directories in path, spyder.exe works as expected.
How to know, what to register?
locate and activate.bat an run it in cmd, then run echo %PATH% and manually register all directories mentioning anaconda.
Alternatively, reinstall anaconda with PATH registratin enabled.
Then you can associate .py files wit spyder.exe and association will work.
System: Windows 11, Python 3.9.7 (Installed through Anaconda3)
This solution will allow you to double click .py files and have them open in the Spyder in the environment of your choice, but does NOT associate .py files with the icon for Spyder.
I'm answering because it took me over an hour to understand & piecemeal together all the (great) solutions that are provided in this thread to get something that works (thanks Martin Sorgel, JoeB152 and Max-K).
NOTE: Some commentors above say to use a Bat-to-Exe converter & that will let you get the icon too.. but, DON'T! All of the ones you're finding via google have got some bad malware in them and my computer ended up deleting the .exe's I was making using that method because they had a Trojan in them.
Full Solution:
STEP 1: Make a .bat file that will launch Spyder in a specific environment.
1.1 Open a plain text editor (e.g. Notepad, etc.) and copy/paste the text below into it.
#echo off
call [YOUR_CONDA_PATH]\Scripts\activate.bat [YOUR_CONDA_PATH]
call conda activate [YOUR ENVIRONMENT]
call start [YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\pythonw.exe "[YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\Scripts\spyder-script.py" %1
Update [YOUR_CONDA_PATH] in the text above with the path to Anaconda3 on your computer. Mine was C:\Users\myusername\Anaconda3\ & yours is likely similar.
1.2 Save this new file as spyderlaunch.bat and place it on your computer somewhere that you'll NEVER move it (otherwise you'll have to do STEP 2 each time you move this file. I save mine in a python_env_settings folder where I keep info on what packages I installed manually in my different environments).
NOTE: JoeB152 says you can remove the second line and remove the environment extension from the third line of the text above if you have Spyder installed in your base environment. I'm not sure if this works...
1.3 Make sure your new .bat files works! It works if when you double click on spyderlaunch.bat, that it launches and opens Spyder in the environment you want it to! (Spyder will show the environment it opens in on the bottom right hand side: ).
STEP 2: Tell your computer to associate (i.e. open) all .py files with the spyderlaunch.bat file you just created.
2.1 Open an Anaconda Terminal with "run as an administrator" (by right clicking on the application) and run the following 2, separate commands. Update[PATH_TO_YOUR.batfile] to wherever you saved spyderlaunch.bat in 1.2.
assoc .py=Python
ftype Python="[PATH_TO_YOUR.batfile]" "%1" %*
Errors?
If you don't run the Anaconda Terminal application as an administrator you will be denied access to associate .py=Python. If that's not your issue, then check that the spaces and quotation marks are exactly where they appear above. In particular, you may want to make sure there is a space in between the quotation marks around [PATH_TO_YOUR.batfile] and those around %1.
I was unable to find a spyder.exe on my installation of conda. However in my users/.anaconda/navigator/scripts I found a spyder.bat file. Using this to open the file opens an anaconda prompt and shortly after spyder will open the file. The file icon is broken but it works for me. Hope this might help.
(Comment in relation to the responses by JoeB152 and Jessica Haskins - I am new, so I cannot leave comments)
I found that their suggested .bat file works once you copy-paste the following file from A to B:
A) C:\Users\USERNAME\Anaconda3\Scripts\spyder-script.py
B) C:\Users\USERNAME\Anaconda3\envs\ENVRIONMENT_NAME\Scripts\
...where ENVIRONMENT_NAME is the name of your environment, such as main or test.
The .bat file contains:
#echo off
call C:\Users\bloggsj\Anaconda3\Scripts\activate.bat C:\Users\bloggsj\Anaconda3\
call conda activate C:\Users\bloggsj\Anaconda3\
call start C:\Users\bloggsj\Anaconda3\envs\main\pythonw.exe "C:\Users\bloggsj\Anaconda3\envs\main\Scripts\spyder-script.py" %1
Then associate .py files with that .bat file (e.g., via the 'Open with...' dialogue).
Alternatively, you could try using in the last line of the .bat file the file path: "C:\Users\bloggsj\Anaconda3\Scripts\spyder-script.py"
Get Spyder by itself:
https://docs.spyder-ide.org/current/installation.html
Set your default file opener to your newly installed spyder
To be able to add packages:
Make sure Anaconda is installed.
Go to Spyder preferences
Go to Python interpreter
Select: "Use the following Python interpreter"
Select file path with Anaconda and hit apply
Now you should be able to open files directed using Spyder and update your environment using Anaconda.
I made a batch utility spy.bat for this (not in the Anaconda directory)
spy.bat
call E:\Anaconda3\Scripts\activate.bat E:\Anaconda3
e:\Anaconda3\python.exe e:\Anaconda3\Scripts\spyder-script.py %1
Then, I associated the .py file type in Windows with this batch file as:
Right click any .py
Choose 'Open With' -> 'More Apps' -> 'Search on your PC' ->'select spy.bat'
In the spy.bat:
E:\Anaconda3 is my Anaconda installation directory.
First line starts the environment (Anaconda prompt).
Second line starts the spyder-script. %1 is to open the pyscript you want to open.

How to remove project in PyCharm?

If I'm closing a project and then just delete the project folder, after PyCharm restarts an empty project folder is created again.
Just follow these steps in order. They assume you currently have the project open in a PyCharm window:
Close your project by clicking on File -> Close Project
Locate your project in the PyCharm file directory
Delete your project's directory
I agree that PyCharm's handling of what should be a very simple procedure is crappy. Maybe this will be improved in the future?
If you want to remove the project from the recent projects list, just highlight the project with your mouse and hit the del key.
This is for PyCharm 2019.1, but I can bet this will work in previous versions.
I am assuming you are in a Unix based operating system (macOs or any Linux distribution). I am also assuming you have currently opened the project you want to remove.
Close the project by going to File → Close Project:
 
Remove the project from Pycharm's Welcome Window:
Locate your project and delete it, you can use several alternatives, but I encourage you to use the following
$ rm -rf /path/to/your/project/directory
The previous command will work in macOS and Linux. In Windows, you can delete it manually by locating the projects directory using File Explorer.
That's it, the project has been completely deleted!
click the project you want to remove.
menu bar Edit-> Remove from Project View.
Version 2017.2.1
Press Alt+1 to bring the project pane, and make sure you're in
Project view (not Project Files view).
Select the desired project, and choose "Delete from project
view" from the context menu (right click). You can also use Delete
key.
There is a difference between PyCharm projects and the list of recently opened projects (File → Open recent...):
The PyCharm project is simply an arbitrary folder1 with the appropriate .idea subfolder in it.
The list of recently opened projects is simply the list of paths to some of such folders.
PyCharm does not keep the list of all projects, so you may safely delete a project folder (or its .idea subfolder) from your file system.
But if you have a bad luck, the path to your project directory is already listed in the list of recently opened projects, and if you try opening it with File → Open recent..., then
in older version of PyCharm, the deleted folder is re-created (as a new folder with only the .idea subfolder in it),
in newer versions of PyCharm you get the prompt similar as
The path C:\Users\you\PycharmProjects\deleted_project does not exist.
If it is on a removable or network drive, please make sure that the drive is
connected.
with buttons "OK" and "Remove From List", so you may simply remove the deleted folder from the list,
in all version of PyCharm, if you deleted only the .idea subfolder, it is silently re-created (but all your project settings are lost, of course).
To prevent from the uncomfortable consequence of the 1st possibility of using the File → Open recent... to re-create already deleted project, follow e.g. the first two instructions in the Simin Jie's answer.
1 generally a subfolder of the PyCharmProjects folder in your user profile directory (e.g. if you created it with File → New Project... and accepted the offered default path).
Works for Windows and MacOS.
File -- Open Recent -- Manage Projects
Cliek X to delete from Recent Projects
Delete local directory
According to https://www.youtube.com/watch?v=ksbmc0coO4k
In PyCharm 2018 I finally had to resort to deleting .idea folder and the project went away.
Highlight the project with your mouse and hit the del key, which works for me.
In 2021 maybe another way to do it. My problem was in project with user directory (root was /Users) that contains too many not Python files, and it triggers some problems with Git and file index.
What I did:
show hidden files (Cmd + Shift + .),
find .idea directories,
delete all .idea directories.
open PyCharm and add project from correct directory.
That's it.
I had the same issue. My solution was:
Close project.
Rename the folder you want to dissapear
Open pycharm project. A dialog will be shown asking if you want Pycharm remove the folder. Answer: yes.
That is enough.
In version 2017.2.1
File → Close Project
You will be taken to the start-up window, where you can see the projects you've created on the left. You can then delete a project by clicking the cross sign ('x') following the project name.

Mac Swampy (Python learning module) install

I thought I would teach my kids programming and am using "Think Python" by Downey. The book is great except when he gets to installing "Swampy" a Turtle-like learning module. I've spent a few hours trying to figure it out--now I need help.
Can anyone offer a clear, step-by-step set of instructions on how to install Swampy? Please assume zero prior knowledge of Unix, etc.
I'm on OS X (10.6.6). Python is running fine. Tkinter is fine.
Here are the overall installation instructions offered at the author's site:
http://www.greenteapress.com/thinkpython/swampy/install.html
I am stuck at the "Add Swampy to your search path" section.
Here are the additional instructions the author points to regarding the search path:
http://docs.python.org/install/index.html#inst-search-path
Probably terribly obvious buy tough stuff for an intro text!
Update
A few details for the record for anyone who comes across this later:
My installation does not have a file or folder "python2" so the pathname "/home/downey/swampy-2.0/python2" might not work. I just dropped the "python2" and all was well.
Had a bit of a problem finding the bashpc or bash_profile file. Didn't seem to exist even when looking for hidden files. So I just created a new one. All seems well.
Thanks again for your help--I would never have figured it out :)
I'm surprised there isn't some sort of installer or script to handle this given the audience for the book (newbies like me). :)
Open the terminal and navigate to the folder where you unzipped Swampy. Find the full directory path of that location by typing pwd. Let's say that location is /home/downey/swampy-2.0/python2. Now in the terminal issue this command export PYHTONPATH="/home/downey/swampy-2.0/python2". This will set the PYTHONPATH environment variable.
If that works fine, you need to make sure that this always runs when you open the terminal. For that you need to edit a file called .bashrc on your computer.
In the terminal, try nano ~/.bashrc.
That will open a simple text editor. In that file add the same line export PYHTONPATH="/home/downey/swampy-2.0/python2". Type Ctrl+X to close and make sure you save.
Now instead of always setting the environment variable manually, you can just do source ~/.bashrc and that will execute the command for you.
Note that every time you exit and reopen the terminal, you need to run source ~/.bashrc for the PYTHONPATH to be set.If you don’t want to run source ~/.bashrc every time you open the terminal, you can configure the Terminal to automatically run this command at startup (i.e. when the terminal window is opened). To do that –
Go to Terminal –> Preferences –> Settings –> Shell.
Check ‘Run Command’ and enter source ~/.bashrc.
NOTE: These settings are only need to be applied to each profile separately. For example if you applied these settings to the Basic profile, they won’t be applicable to Grass profile.
Hope this helps!
An easy way to add variables to your path is to add them to your .bash_profile, which is located in your home directory. This file loads every time you open up terminal. It is a hidden file, so you will not be able to view it (by default) in Finder. Run this command after opening a new terminal window:
open -a TextEdit .bash_profile
This will open the profile up in a new TextEdit window. Add this & save:
PYTHONPATH=${PYTHONPATH}:/home/downey/swampy-2.0/python2
export PYTHONPATH
Assuming your Swampy download path is the same from the Web Site (/home/downey/swampy-2.0/python2), the next time you run terminal, the path should now be included.

Categories

Resources